Reputation: 1511
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:
I am getting the following error:
Not found :( Sorry, but the page you were trying to view does not exist.
Upvotes: 2
Views: 701
Reputation: 140
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);
Upvotes: 2
Reputation: 24619
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);
Upvotes: 1