Another ASP.Net WebAPI route not found

Firstly, I read as many articles as I can find on this topic, and installed several plugins for route debugging. I'm more familiar with Java / Spring, so I really don't know how to debug this thing using vs 2012. (I still can't get IISExpress to print any debugging much less than the debug output I'm 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 can get to the index page using the default controller. However, I am trying to click 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 others 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 
+6
source share
2 answers

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

So, while for regular MVC routes, the RouteConfig class is used

 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 uses WebApiConfig, which is deleted from the code suggested above in the static WebApiConfig class:

 public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } 
+5
source

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

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

By the way, what is a BreezeController and why is it designed on WebApiController?

+1
source

Source: https://habr.com/ru/post/946230/


All Articles