Catch 404 Error in MVC4

前端 未结 2 1746
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 04:54

I have an problem cacting the HTTP Error 404.0 - Not Found. I have turned on the


              


        
2条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 05:33

    Have you implemented an ErrorController that handles the redirects in your customErrors attributes? In your case, it would look something like this:

    public class ErrorController : Controller
    {
        public ActionResult General()
        {
            return View();
        }
    
        public ActionResult HttpError404()
        {
            return View();
        }
    
        public ActionResult HttpError500()
        {
            return View();
        }
    }
    

    You would also need to implement the views for each of the ActionResults in your Views folder.

    In terms of logging the error, you would need to implement this in the Application_Error event handler as follows:

    protected void Application_Error()
    {
        var exception = Server.GetLastError();
        var httpException = exception as HttpException;
        // Do logging here
    }
    

    This is to "catch" the error before the 302 redirect to your Error pages defined in the ErrorController above.

提交回复
热议问题