How to handle WebApi 404 error

I am using an MVC 5 application with WebApi2 in the same domain. My problem now is how to handle 404 error in WebApi. I use routing in my WebApi.

[RoutePrefix("myapi")] public class MyApiController : ApiController { [Route("value")] public string myvalue() { return "value"; } } 

Now I have a url "/myapi/value" that returns a string of values. The problem is that I requested "/myapi/value/bla/bla" or any URL that is not in my API, it returns a 404 error.

I tried this link http://weblogs.asp.net/imranbaloch/handling-http-404-error-in-asp-net-web-api , it applies only to the WebApi project, but not to the MVC + WebApi project.

Please, help. TIA

UPDATE:

Please read carefully !!!!

This will be the default page if you are not processing 404 in your WebApi. It includes your physical path.

enter image description here

+4
source share
1 answer

Updated:

I think the following has a more detailed description of how to implement error handling for the WebApi 2+ project: http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling .

There are two problems in my projects when combining MVC and WebApi in one project.

The first is that my error handling on my regular MVC pages no longer worked. In order to solve this problem, I first initialized WebApi routing by MVC controller routing and made small adjustments in the web.config file, as described in your post.

Secondly, if you want to handle invalid webapi routes, for example, the correct directory, but with incorrect values โ€‹โ€‹that cannot be resolved. try the solution in the Global Error Handling section of the link below: http://weblogs.asp.net/jongalloway/looking-at-asp-net-mvc-5-1-and-web-api-2-1-part- 4-web-api-help-pages-bson-and-global-error-handling

+1
source

All Articles