开发者

ASP.NET MVC: How to serve content while returning status code 404? [duplicate]

开发者 https://www.devze.com 2022-12-25 07:25 出处:网络
This question already has an answer here: Closed 11 years ago. Possible Duplicate: How to configure IIS to serve my 404 response with my custom content?
This question already has an answer here: Closed 11 years ago.

Possible Duplicate:

How to configure IIS to serve my 404 response with my custom content?

I would like to serve a user friendly "not found" page in my ASP.NET MVC application while providing a 404 status code. (based on this answer)

I already have the mechanism how to catch an invalid route and the custom 404 page is served by my ErrorController/Handle404 action.

My current implementation of Handle404:

    public ActionResult Handle404()
    {
        Response.StatusCode = 404;
        return View("NotFound"开发者_开发技巧);
    }

Currently, IIS serves the page as 404 - File or directory not found. The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable. (the standard IIS page, not my user friendly content)

How can I include the 404 status code in the result served by the Handle404 action while still serving the content?


Setting

 Response.TrySkipIisCustomErrors = true; 

did the trick.


You are already doing the correct thing. You can look at Fiddler to confirm this.

However, there is no guarantee that a browser will honor your content. Wikipedia notes:

Internet Explorer (before Internet Explorer 7), however, will not display custom pages unless they are larger than 512 bytes, opting to instead display a "friendly" error page. Google Chrome includes similar functionality, where the 404 is replaced with alternative suggestions generated by Google algorithms, if the page is under 512 bytes in size.

Some trial and error and massaging of your view may be necessary in order to come up with a page which various browsers will actually elect to display.

0

精彩评论

暂无评论...
验证码 换一张
取 消