How to return a view for HttpNotFound() in ASP.Net MVC 3?

前端 未结 6 1040
忘掉有多难
忘掉有多难 2020-11-28 22:47

Is there a way to return the same view every time a HttpNotFoundResult is returned from a controller? How do you specify this view? I\'m guessing configuring a 404 page in

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 23:25

    Here is true answer which allows fully customize of error page in single place. No need to modify web.confiog or create sophisticated classes and code. Works also in MVC 5.

    Add this code to controller:

            if (bad) {
                Response.Clear();
                Response.TrySkipIisCustomErrors = true;
                Response.Write(product + I(" Toodet pole"));
                Response.StatusCode = (int)HttpStatusCode.NotFound;
                //Response.ContentType = "text/html; charset=utf-8";
                Response.End();
                return null;
            }
    

    Based on http://www.eidias.com/blog/2014/7/2/mvc-custom-error-pages

提交回复
热议问题