我希望將所有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