我明白這個問題是有點老了,但是這可能幫助別人。
對於401,您可能會看到標準401未授權頁面,即使您已將401添加到web.config中的customerrors部分。我讀到,當使用IIS和Windows身份驗證時,檢查發生在ASP.NET甚至看到請求之前,因此您會看到Cassini上的空白頁面以及IIS上它自己的401.
對於我的項目,我編輯了Global.asax文件重定向到我爲401錯誤創建的路由,將用戶發送到「未經授權查看此」視圖。
在Global.asax:
void Application_EndRequest(object sender, System.EventArgs e)
{
// If the user is not authorised to see this page or access this function, send them to the error page.
if (Response.StatusCode == 401)
{
Response.ClearContent();
Response.RedirectToRoute("ErrorHandler", (RouteTable.Routes["ErrorHandler"] as Route).Defaults);
}
}
,並在Route.config:
routes.MapRoute(
"ErrorHandler",
"Error/{action}/{errMsg}",
new { controller = "Error", action = "Unauthorised", errMsg = UrlParameter.Optional }
);
,並在控制器:
public ViewResult Unauthorised()
{
//Response.StatusCode = 401; // Do not set this or else you get a redirect loop
return View();
}
你的意思是,如果我的應用程序遷移到IIS我應該看到自定義頁面?或者我應該只看到標準的IIS 401頁面? – 2009-09-14 17:29:06
你會看到401。 – 2009-09-14 17:30:41