我明白这个问题是有点老了,但是这可能帮助别人。
对于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