I am trying to use WebAPI as part of an MVC5 application. The application has areas, so to keep with the existing structure I have created and "API" area. However, I am having some routing troubles as I get a 404 when hitting the API URL.
I have read a number of posts on this, mostly MVC4 examples and so far I have not had any success.
Initially, I had the API routes as part of the area routing, so above the below default route, I had another with the RouteTemplate API/{controller}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"API_default",
"API/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
I then realised my error and moved the API route to a different method in the same area registration file
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Direct_API",
routeTemplate: "API/{controller}"
);
}
I have now moved the API routes out of the area registration file completely, created a new API route config file in app_start and registered it in global.asax before the application routes file. However, still, if I goto http://localhost:port/API/Import I do not hit the API Controller.
The API Import Controller sits in the API area of the application
public class ImportController : ApiController
{
[HttpGet]
public Array Get()
{
return new[]{true, false, false, true};
}
}
Global.asax registration
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
I have also tried registering the file MVC4 way which was WebApiConfig.Register(GlobalConfiguration.Configuration);