没有 Windows 身份验证的 MVC 中的自定义 401 错误页面
Custom 401 Error page in MVC without Windows Authentication
我想在 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 上搜索了这个,但这些答案对我不起作用。
您可以尝试禁用 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>
我想在 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 上搜索了这个,但这些答案对我不起作用。
您可以尝试禁用 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>