我希望将所有 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 状态
到目前为止,我尝试使用此 SO 答案中的建议,因为我使用的是 IIS Express,但无济于事。我尝试使用组合<custom errors>
但<httpErrors>
没有运气 - 仍然显示标准错误或空白页。
根据上述 SO 问题的链接,该httpErrors
部分目前看起来像这样(我还找到了另一个非常有希望的答案,但是没有运气 - 空白回复)
<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>
我什至根据来自iis.netapplicationhost.config
的信息修改了文件并修改为。在我努力的过程中,我还设法偶然发现了这个错误,如另一个 SO question中所述。<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">
<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated">
如何在 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");
}