asp.net-mvc-3 – 如何在Asp.Net Mvc 3中显示自定义错误页面?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 如何在Asp.Net Mvc 3中显示自定义错误页面?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将所有401错误重定向自定义错误页面。我最初在我的web.config中设置了以下条目。
<customErrors defaultRedirect="ErrorPage.aspx" mode="On">
  <error statusCode="401" redirect="~/Views/Shared/AccessDenied.aspx" />
</customErrors>

当使用IIS Express时,我收到库存IIS Express 401错误页面

在不使用IIS Express的情况下,返回空白页。使用Google Chrome的“网络”选项卡检查响应,我看到当页面为空时,标题中返回了401状态

到目前为止我所尝试的是使用this SO answer的建议,因为我使用IIS Express,但无济于事。我已经尝试使用<自定义错误>和< httpErrors>没有运气 – 标准错误或空白页仍然显示

现在的httpErrors部分看起来像@L_404_1@的the link(我也发现另外一个非常promising answer,但是没有运气 – 空白的回应)

<system.webServer>
  <httpErrors  errorMode="DetailedLocalOnly" existingResponse="PassThrough" >
    <remove statusCode="401"  />
    <error statusCode="401" path="/Views/Shared/AccessDenied.htm" />
  </httpErrors>

 <!-- 
 <httpErrors  errorMode="Custom" 
             existingResponse="PassThrough" 
             defaultResponseMode="ExecuteURL">
      <remove statusCode="401"  />
  <error statusCode="401" path="~/Views/Shared/AccessDenied.htm" 
         responseMode="File" />
 </httpErrors>
 -->
</system.webServer>

我甚至修改了applicationhost.config文件修改了< httpErrors lockAttributes =“allowAbsolutePathsWhenDelegated,defaultPath”>到< httpErrors lockAttributes =“allowAbsolutePathsWhenDelegated”>基于iis.net的信息。在我的努力过程中,我还设法在another SO question中描述了这个错误

如何在Asp.Net Mvc 3中显示自定义错误页面

附加信息

以下控制器操作已使用特定用户的Authorize属性进行装饰。

[HttpGet]
[Authorize(Users = "domain\\userXYZ")]
public ActionResult Edit() 
{
   return GetSettings();
}

[HttpPost]
[Authorize(Users = "domain\\userXYZ")]
public ActionResult Edit(ConfigurationModel model,IList<Shift> shifts)
{
    var temp = model;
    model.ConfiguredShifts = shifts;
    EsgConsole config = new EsgConsole();

    config.UpdateConfiguration(model.ToDictionary());
    return RedirectToAction("Index");
}

解决方法

我使用这些步骤:
// in Global.asax.cs:
        protected void Application_Error(object sender,EventArgs e) {

            var ex = Server.GetLastError().GetBaseException();

            Server.ClearError();
            var routeData = new RouteData();
            routeData.Values.Add("controller","Error");
            routeData.Values.Add("action","Index");

            if (ex.GetType() == typeof(HttpException)) {
                var httpException = (HttpException)ex;
                var code = httpException.GetHttpCode();
                routeData.Values.Add("status",code);
            } else {
                routeData.Values.Add("status",500);
            }

            routeData.Values.Add("error",ex);

            IController errorController = new Kavand.Web.Controllers.ErrorController();
            errorController.Execute(new RequestContext(new HttpContextWrapper(Context),routeData));
        }

        protected void Application_EndRequest(object sender,EventArgs e) {
            if (Context.Response.StatusCode == 401) { // this is important,because the 401 is not an error by default!!!
                throw new HttpException(401,"You are not authorised");
            }
        }

和:

// in Error Controller:
    public class ErrorController : Controller {

        public ActionResult  Index(int status,Exception error) {
            Response.StatusCode = status;
            return View(status);
        }

        protected override void Dispose(bool disposing) {
            base.Dispose(disposing);
        }
    }

AND错误文件夹中的索引视图:

@* in ~/Views/Error/Index.cshtml: *@

@model Int32    
@{
    Layout = null;
}    
<!DOCTYPE html>    
<html>
<head>
    <title>Kavand | Error</title>
</head>
<body>
    <div>
        There was an error with your request. The error is:<br />
        <p style=" color: Red;">
        @switch (Model) {
            case 401: {
                    <span>Your message goes here...</span>
                }
                break;
            case 403: {
                    <span>Your message goes here...</span>
                }
                break;
            case 404: {
                    <span>Your message goes here...</span>
                }
                break;
            case 500: {
                    <span>Your message goes here...</span>
                }
                break;
            //and more cases for more error-codes...
            default: {
                    <span>Unknown error!!!</span>
                }
                break;
        }
        </p>
    </div>
</body>
</html>

AND – 最后一步:

<!-- in web.config: -->

<customErrors mode="Off"/>
原文链接:https://www.f2er.com/aspnet/253901.html

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