【发布时间】:2011-10-07 15:57:14
【问题描述】:
我希望将所有 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,则会返回空白页。使用谷歌浏览器的网络选项卡检查响应,我看到当页面为空白时,标题中返回 401 状态
到目前为止,我尝试使用来自this SO answer 的建议,因为我使用的是 IIS Express,但无济于事。我尝试使用<custom errors> 和<httpErrors> 的组合,但没有成功 - 仍然显示标准错误或空白页。
httpErrors 部分目前基于above SO question 中的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 文件,并根据iis.net 的信息将<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath"> 修改为<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated">。在我努力的过程中,我还设法偶然发现了这个错误,如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");
}
【问题讨论】:
-
我还想知道如何显示自定义错误(在我的情况下是 403 错误)- 500 错误工作正常...
-
制作了一个小型库来简化此操作。可用:github.com/Buildstarted/Errlusion
-
只是觉得你可能有兴趣看到这个SO post
-
@CBRRacer 该帖子实际上解决了我遇到的问题,不确定 OP。此外,这个问题的答案都没有提到所需的所有配置选项,因此很难奖励赏金......
-
获得了赏金,但没有一个答案是我想要的,@CBRRacer 的链接中有更好的答案
标签: asp.net-mvc-3 error-handling iis-express custom-error-pages http-error