0

我已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);
        }
    }
}
4

1 回答 1

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

     <%@ 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>
于 2017-03-12T07:37:46.903 回答