我已按以下方式设置我的Web.config
以显示自定义403错误页面但由于某种原因页面未显示 - 我仍然收到默认错误页面!自定义页面针对使用相同404
ErrorController
正确显示
的Web.Config
<customErrors mode="On" defaultRedirect="~/Error">
<error redirect="~/Error/NotFound" statusCode="404" />
<error redirect="~/Error/Forbidden" statusCode="403" />
</customErrors>
ErrorController
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);
}
}
}
答案 0 :(得分:0)
<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&#34;错误&#34;
<%@ 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>