I have added config.Formatters.Remove(config.Formatters.XmlFormatter);
to the Register
method of WebApiConfig.cs in an ASP.NET Web API project. This works nicely to return JSON when a 404 Not Found exception occurs. I get a message like the following:
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:2034/api/dfdf'.","MessageDetail":"No type was found that matches the controller named 'dfdf'."}
Is it possible to override this message to return a custom one? If so, how is it done? I'm using Web API 2.
It seems simple, but there are a bunch of places where ASP.Net automatically creates 404s (rather than your controller returning a 404 response manually).
This article shows how to implement a custom IHttpControllerSelector
and IHttpActionSelector
that uses a typical ApiController
to generate your custom 404 HttpResponseMessage
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.