简体   繁体   中英

How to return custom 404 response when id not found

I always wonder what is the best error response code in folowing situation:

public IActionResult Index(Guid id)
{
    var entity = _dbContext.Entities.Find(id);
 
    if (entity == null)
    {
         return NotFound(); //??
    }
    return View(entity)
}

404 - Not Found seems most appropriate, however, from debugging point of view, non-existing ID and wrong controller/action names are very different errors.

Anyway, I've decided to return custom error page with more explanatory message, so I can differentiate between 404 and 404.

How to return custom 404 page from Controller's Action and default 404 in other cases?

I would like to avoid returning HttpResponseMessage, since I would need to change return type of the Action.

PS: In comments you may vote for 404 resp other response code you would use in this particular case.

Take a look at this question first.

Theoreticaly you shouldn't use HttpStatus-codes as Application Error Codes.

However, on a public website, 404 has one specific meaning:

The requested resource could not be found but may be available in the future. Subsequent requests by the client are permissible.

Calling /getresource/347d2f3a-bd0f-4d0b-8c05-2e7f3f8f265e is a resource. If this is not available you should use 404. If it is available, then 200. That's perfectly fine.

Google even says, you should send 404. From the google support :

Returning a code other than 404 or 410 for a non-existent page (or redirecting users to another page, such as the homepage, instead of returning a 404) can be problematic. Firstly, it tells search engines that there's a real page at that URL. As a result, that URL may be crawled and its content indexed. Because of the time Googlebot spends on non-existent pages, your unique URLs may not be discovered as quickly or visited as frequently and your site's crawl coverage may be impacted (also, you probably don't want your site to rank well for the search query

For the implementation, I would just throw a custom NotFoundException (If you want to setup more meaningfull information) with all the data you need, and handle it globaly through the ExceptionFilterAttribute . There you can turn it into a 404 response.

Of course you can let the original exception just bubble up to the ExceptionFilterAttribute , but then you have not that many possibilites for making it meaningfull.

You can utilize the CreateResponse extension method and would do something as follows:

return Request.CreateResponse(HttpStatusCode.NotFound, "foobar");

I've used the HttpStatusCode.NoContent for something like this before. It really depends on what your business logic is and how you want to handle these cases. NoContent will result in a successful http call where as NotFound will be an error. NotFound will trigger your default MVC error page route(if you have that setup) and NoContent won't. Is this a response that is possible through normal use/traversing of the app or is it only something that will occur if a malicious user is tampering with urls? All of those pieces are what I take into consideration determining which http status code I want to return. Hope this helps!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM