Yet another ASP.Net WebAPI route not found
Asked Answered
S

2

6

First off I have read as many articles as I can find on this topic and installed several "route debug" plugins. I am more familiar with Java/Spring so I really have no idea how to debug this thing, using vs 2012. (I cannot anyway to make IISExpress print any debug much less the kind of the debug output I am used to with Spring/Tomcat.)

public class RouteConfig
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  

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

Now, I am able to hit the Index page via the default controller. However, I am trying to hit the URL /WebApi/Metadata/ based on the following controller:

[BreezeController]
public class WebApiController : ApiController {
  private readonly EFContextProvider<BankruptcyDbContext> _contextProvider =
    new EFContextProvider<BankruptcyDbContext>();

  [HttpGet]
  public string Metadata() {
    return _contextProvider.Metadata();
  }
}

The "Route Debugger" says that my requests for /WebApi/Metadata, /WebApi/Metadata/, /WebApi/Metadata/0, and more should "match" but all I get is 404.

Edit1: I finally found the trace logs and got a little more detail:

The controller for path &amp;#39;/WebApi/Metadata&amp;#39; was not found or does not implement IController
Sock answered 31/5, 2013 at 2:44 Comment(0)
I
5

Be sure you are using the current latest version of Visual Studio 2012 with Update 2 etc.. You should not only have a RouteConfig.cs file in App_Start, but also there is a WebApiConfig.cs file

So While Normal MVC routes use the RouteConfig class

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

The Web API is using the WebApiConfig , which is stubbed out with the out of the box code suggested above in the static class WebApiConfig:

public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}
Iodic answered 5/6, 2013 at 6:52 Comment(0)
E
1

You would need to register routes using the MapHttpRoute extension for ApiController based routes. Example:

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

BTW, what is BreezeController and why is it decorated on WebApiController?

Ectomorph answered 31/5, 2013 at 3:21 Comment(3)
Awesome, that worked! I didn't know there was a different between mapRoute and MapHttpRoute.Sock
@BrockNoland Welcome to the confusing world of ASP.NET Web API vs ASP.NET MVC. Two completely different frameworks, packaged together and made to look extremely similar "to ease the learning curve".Abroach
@Ectomorph - The [BreezeController] attribute configures the formatter and supplements the OData IQueryable processing for Breeze clients.Chrystalchryste

© 2022 - 2024 — McMap. All rights reserved.