我希望所有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>
的组合,但仍然无法解决 - 显示标准错误或空白页面。
httpErrors
部分目前看起来像此链接所示,基于上述SO问题(我还找到了另一个非常有希望的答案,但没有成功 - 返回空白)。<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");
}