asp.net-mvc – 在Azure中启用自定义错误

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 在Azure中启用自定义错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法设置IIS来为我的自定义错误页面提供MVC管道之外的错误.如果我在控制器中抛出一个异常,它就是所有文件,Application_Error事件处理:
protected void Application_Error(object sender,EventArgs e)
    {
        var error = Server.GetLastError();

        var routeData = new RouteData();
        routeData.Values["Controller"] = "Error";
        routeData.Values["Area"] = "";
        routeData.Values["Exception"] = error;

        if (error is HttpException)
        {
            switch (((HttpException)error).GetHttpCode())
            {
                case 401:
                    routeData.Values["Action"] = "NotAllowed";
                    break;

                case 403:
                    routeData.Values["Action"] = "NotAllowed";
                    break;

                case 404:
                    routeData.Values["Action"] = "NotFound";
                    break;
                default:
                    routeData.Values["Action"] = "ServerError";
                    break;
            }
        }
        else
        {
            routeData.Values["Action"] = "ServerError";
        }

        Response.Clear();
        Server.ClearError();

        IController controller = new ErrorController();
        controller.Execute(new RequestContext(new HttpContextWrapper(((MvcApplication)sender).Context),routeData));
    }

但是,如果我浏览到一个不存在的URL,IIS将处理404错误(或任何其他错误),给我标准的IIS错误消息,并完全忽略我的web.config设置:

<system.web>
  <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/error">
    <error statusCode="401" redirect="/error/notallowed" />
    <error statusCode="403" redirect="/error/notallowed" />
    <error statusCode="404" redirect="/error/notfound" />
    <error statusCode="500" redirect="/error/servererror" />
  </customErrors>
</system.web>

<system.webServer>
  <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
    <clear />
    <error statusCode="401" path="/error/notallowed" />
    <error statusCode="403" path="/error/notallowed" />
    <error statusCode="404" path="/error/notfound" />
    <error statusCode="500" path="/error/servererror" />
  </httpErrors>
</system.webServer>

/ error / *由我的应用程序内的控制器处理.我该怎么做才能使IIS执行自定义错误路径,而不是给我标准的错误页面

这是在Azure上运行的ASP.NET MVC 3.它在直接IIS下也不起作用,但开发服务器确实执行控制器.

解决方法

IIS不尊重defaultResponseMode.您必须将responseMode添加到每个错误中,如下所示:
<httpErrors errorMode="Custom">
  <clear />
  <error statusCode="401" path="/error/notallowed" responseMode="ExecuteURL" />
  <error statusCode="403" path="/error/notallowed" responseMode="ExecuteURL" />
  <error statusCode="404" path="/error/notfound" responseMode="ExecuteURL" />
  <error statusCode="500" path="/error/servererror" responseMode="ExecuteURL" />
</httpErrors>

为什么默认不适用超出我的范围.

原文链接:https://www.f2er.com/aspnet/251297.html

猜你在找的asp.Net相关文章