I've created a default MVC 5 Web application and want to allow clients to call that API. For reasons too long to get into, I really need to keep the API as part of the application. I first added an additional controller to the default solution called FooController that returns a View with some dummy data. This should have no bearing on the Web API, but I wanted to make sure I could at least add a MVC controller. I can run the solution and when browse to http://localhost:54110/Foo my dummy data is returned
In a separate solution, I was able to use Getting Started with ASP.NET Web API 2 to create an API. I'm able to call the api using: http://localhost:47503/api/products/2 which sends me a json file. All is good.
I went back to my first project and repeated the steps for creating the Web API2, however when I try and call the API via http://localhost:54110/api/products/2 I'm getting a 404. Obviously I have a routing problem.
Here is my Application_Start
, where I added last line to call GlobalConfiguration
as that is what is used in the Web API sample.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configure(WebApiConfig.Register);
}
and here is the Register method from WebApiConfig
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
I know this is being called, because the debugger is breaking on this method. Any ideas as to what I'm doing wrong? Is there a way to trace what is happening when I hit ENTER after http://localhost:54110/api/products/2 ?