【问题标题】:How to handle 404 error in mvc?如何处理 mvc 中的 404 错误?
【发布时间】:2018-03-07 05:24:17
【问题描述】:

我正在尝试处理 MVC 中的 404 错误,所有其他错误正在使用 Angular 进行处理,因此不必担心其他异常。

我正在像这样处理 Global.asax 页面中的 404 错误

 protected void Application_Error()
        {
            // if (Context.IsCustomErrorEnabled)
             ShowCustomErrorPage(Server.GetLastError());
        }
        private void ShowCustomErrorPage(Exception exception)
        {
            var httpException = exception as HttpException ?? new HttpException(500, "Internal Server Error", exception);

            Response.Clear();
            var routeData = new RouteData();
            routeData.Values.Add("controller", "ErrorHandling");
            routeData.Values.Add("fromAppErrorEvent", true);

            switch (httpException.GetHttpCode())
            {
                case 404:
                    routeData.Values.Add("action", "HttpError404");
                    break;
            }

            Server.ClearError();

            IController controller = new ErrorHandlingController();
            controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        }

但是当 404 错误发生时 Execute 方法

controller.Execute(new RequestContext(new HttpContextWrapper(Context), 路由数据));

呈现页面的 html 而不是页面本身,如何进行更改以便我能够呈现页面本身而不是页面的 html。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 c#-4.0 error-handling


    【解决方案1】:

    在您的网络配置中编写以下代码

    <customErrors mode="On" >
       <error statusCode="404" redirect="~/Controller/CustomErrorPage" />
    </customErrors>
    

    【讨论】:

    • 我正在使用我的 Angular 应用程序处理所有剩余的错误,我只想处理 404 错误,我该怎么做,感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2016-06-05
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多