2

I have a mvc application with an index page(static html) under the Home view. I could view the page. The routeconfig is :

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

I am adding a WebAPI controller and am trying to invoke the Get.

public class TopicsController : ApiController
    {
         public string Get()
        {
            return "Hello WebAPI";
        }

    }

The route config in Webapi is :

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

Added the application start function :

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();


            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

When i try the following:

http://localhost:11001/api/Topics

I am getting the following error:

Not found :( Sorry, but the page you were trying to view does not exist.

2
  • Please can you show us how your config.Routes.MapHttpRoute() is being called? Is it being called from your application's Global.asax file? Commented May 16, 2017 at 13:13
  • @Luke i have added the application start function Commented May 16, 2017 at 13:25

2 Answers 2

2

Take a look at the default project from visual studio, you will notice that the order you add the routes is very important, because 'api/topics' algo matches the mvc route, that is why it tells you that it doesn't exist, because you don't have a 'api' controller.

Change it to this:

AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

leave the rest like you have it

EDIT:

Updated the answer to reflect the WebAPI version from the question.

But in the last version of WebAPI:

WebApiConfig.Register(GlobalConfiguration.Configuration);

should be:

GlobalConfiguration.Configure(WebApiConfig.Register);
Sign up to request clarification or add additional context in comments.

3 Comments

GlobalConfiguration.Configure(WebApiConfig.Register); is throwing an error stating "Error 1 Non-invocable member 'System.Web.Http.GlobalConfiguration.Configuration' cannot be used like a method.". Also i could not find the GlobalConfiguration.Configure method, instead only configuration was available.
Most probably is a matter of mvc versions: use WebApiConfig.Register(GlobalConfiguration.Configuration);
Take a look at the default project from Visual Studio thanks
1

Try to change registration order in Global.asax

AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.