【问题标题】:Custom 401 Error page in MVC without Windows Authentication没有 Windows 身份验证的 MVC 中的自定义 401 错误页面
【发布时间】:2017-05-30 10:10:01
【问题描述】:

我想在 ASP.NET MVC 中针对 401 显示我的 web.config 错误页面,只有 404 和默认错误页面有效。身份验证过程是我自己的,不使用 .net 身份框架。

我在控制器操作方法中抛出了新的 401 异常。

行动:

[HttpGet]
[HandleError]
public ActionResult Delete(int id)
{
  //if (user.IsInRole(RoleType.DELETE))
  if (myOwnUserClass.IsInRole(RoleType.DELETE))
  {
      return View();
  }
  else
  {
      return new HttpStatusCodeResult(401);
      //return PartialView("_unauthorize");
  }
}   

web.config

<customErrors mode="On" defaultRedirect="~/Error/Index">
      <error statusCode="401" redirect="~/Error/Unauthorized" />
      <error statusCode="404" redirect="~/Error/NotFound"/>
    </customErrors>

我得到的是默认的 401 错误页面,不是我自己的。

Default 401 error page similar to this

我也为此搜索了 Stack Overflow,但这些答案对我不起作用。

【问题讨论】:

    标签: asp.net-mvc custom-error-pages


    【解决方案1】:

    您可以尝试禁用customErrors,而是使用httpErrors 来处理这些错误。

    <system.web>
    
      <customErrors mode="Off" />
    
      <!-- other tags removed for brevity -->
    </system.web>
    <system.webServer>
    
      <httpErrors errorMode="Custom" existingResponse="Auto">
        <clear />
        <error statusCode="401" responseMode="ExecuteURL" path="/Error/Unauthorized" />
        <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
        <error statusCode="500" responseMode="ExecuteURL" path="/Error" />
      </httpErrors>
    
      <!-- other tags removed for brevity -->
    </system.webServer>
    

    【讨论】:

    • hmmm 让我通过 httpErrors 检查一下,并会回复你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多