我已Web.config
按以下方式设置我的显示自定义 403 错误页面,但由于某种原因该页面未显示 - 我仍然得到默认错误页面!自定义页面正确显示,404
其使用相同ErrorController
网络配置
<customErrors mode="On" defaultRedirect="~/Error">
<error redirect="~/Error/NotFound" statusCode="404" />
<error redirect="~/Error/Forbidden" statusCode="403" />
</customErrors>
错误控制器
public class ErrorController : BaseController
{
public ViewResult Forbidden()
{
Response.StatusCode = 403; //you may want to set this to 200
return View("Error403");
}
}
生成 403 的属性:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AuthorizeEMAttribute : System.Web.Mvc.AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAuthenticated)
{
filterContext.Result = new System.Web.Mvc.HttpStatusCodeResult((int)System.Net.HttpStatusCode.Forbidden);
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}
}