我希望将所有401错误重定向到自定义错误页面。我最初在我的web.config中设置了以下条目。如何在Asp.Net Mvc 3中显示自定义错误页面?
<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
部分看起来像这样在基于the link的那一刻起above SO question(我还发现另一个非常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中显示自定义错误页面?
其他信息
下列控制器动作已经装饰了Authorise
属性为特定用户。
[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错误工作正常... –
作出一个小型图书馆,使这更容易。它是可用的:https://github.com/Buildstarted/Errlusion – Buildstarted
只是想你可能有兴趣看到这[SO帖子](http://stackoverflow.com/questions/6308186/custom-errors-not-working-with - iisexpress) – CBRRacer