How to catch undefined api call in ASP.NET MVC4 Web API

ASP.NET/Mono MVC4 Web API v.1 Application.

How to catch calls to undefined api methods. Calling http://localhost:52216/erp/api/undefinedmethod returns an error in the browser:

 <Error> <Message> No HTTP resource was found that matches the request URI 'http:// localhost:52216/erp/api/undefinedmethod'. </Message> <MessageDetail> No type was found that matches the controller named 'undefinedmethod'. </MessageDetail> </Error> 

How to catch this error to enter the database? I tried the code from the question

How to register ALL exceptions worldwide for a C # MVC4 WebAPI application?

but the Application_Error code and exception code are still not running, the error is returned by the browser. How to catch this error?

If url does not have api, for example

 'http://localhost:52216/erp/undefinedmethod' 

Error_ application completed correctly.

The WebAPI configuration from the VS2013 WebAPI project template is used:

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

Update

I tried changing the answer. API controllers use Forms authorization and are decorated with a standard [Authorize] attribute. If authorization fails, the standard xml api error message

 <Error> <Message>Authorization has been denied for this request.</Message> </Error> 

going on. How to catch this error also?

+6
source share
1 answer

Imran Beloch wrote an article on how to do this. Basically you need to create your own HttpControllerSelector and HttpActionSelector. You can find the article here .

EDIT:

If your application uses routes other than those registered in WebApiConfig, you need to make some changes to the routing. Instead of defining the Error404 path at the end of the Register method, define a new method (RegisterNotFound) to register the route:

 public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } public static void RegisterNotFound(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "Error404", routeTemplate: "{*url}", defaults: new { controller = "Error", action = "Handle404" } ); } } 

And then in the Global.asax register, call this method last:

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); WebApiConfig.RegisterNotFound(GlobalConfiguration.Configuration); GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new HttpNotFoundAwareDefaultHttpControllerSelector(GlobalConfiguration.Configuration)); GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpActionSelector), new HttpNotFoundAwareControllerActionSelector()); } 
+4
source

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


All Articles