[英]Custom 403 error page in MVC4 not showing
我已經按照以下方式設置了Web.config
來顯示自定義的403錯誤頁面,但是由於某種原因,該頁面未顯示-我仍然會獲得默認錯誤頁面! 對於使用相同ErrorController
404
,正確顯示了自定義頁面
Web配置
<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);
}
}
}
<customErrors mode="On" defaultRedirect="~/Error/Error.html" redirectMode="ResponseRewrite">
<error redirect="~/Error/403.aspx" statusCode="403" />
<error redirect="~/Error/404.aspx" statusCode="404" />
<error redirect="~/Error/500.html" statusCode="500" />
</customErrors>
在根目錄“錯誤”中創建頁面403.aspx
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>403 Not Found</title>
</head>
<body>
<%
Server.ClearError();
Response.Status = "403 - Forbidden: Access is denied.";
Response.StatusCode = 403;
%>
<div>
<h2>403 - Forbidden: Access is denied.</h2>
</div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.