0

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 ?

WhiskerBiscuit
  • 4,795
  • 8
  • 62
  • 100
  • what does your mvc routing look like? also what does the method from your api look like? – Ric Oct 15 '15 at 10:27
  • This help? http://stackoverflow.com/questions/11990036/how-to-add-web-api-to-an-existing-asp-net-mvc-4-web-application-project – Andy Wiesendanger Oct 15 '15 at 13:13
  • It's possible you just haven't set something up right, but given that you're still in a very early stage of development here, it seems more trouble than it's worth to try to figure out what if anything is misconfigured. Create a new project and add both Web Api and MVC from the start. Then, just move over what little code you've developed. – Chris Pratt Oct 15 '15 at 13:20
  • Doh! -- I didn't notice that option. I'll check it out later. – WhiskerBiscuit Oct 15 '15 at 17:08
  • It would be good if you provide action methods. 404 might also be because action methods, parameters binding mismatc. How are you testing the api url?? – Mithun Pattankar Oct 15 '15 at 17:30

0 Answers0