返回自定义错误

Vic*_*scó 5 c# asp.net-mvc iis-7 asp.net-mvc-4

我设计了一个控制器ErrorController,使用类似Forbiddenor 的方法调用NotFound,因此我可以将以下几行添加到 Web.config 中:

<customErrors mode="On" defaultRedirect="~/Error/Unknown" />
    <error statusCode="404" redirect="~/Error/NotFound" />
    <error statusCode="403" redirect="~/Error/Forbidden" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

所以现在我希望能够做这样的事情:

public ActionResult Edit(int idObject)
{
    if( user.OnwsObject(idObject) )
    {
        // lets edit
    }
    else
    {
        // ** SEND AN ERROR 403 ***
        // And let ASP.NET MVC with IIS manage that error to send
        // the requester to the Web.config defined error page.
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是我尝试过类似的事情:(A)throw new HttpException(403, "Error description");:导致导致系统崩溃的未处理异常,(B)return HttpStatusResultCode(403, "Error description"):导致这些错误的系统默认页面。

我应该用什么代替?

提前致谢。

Dav*_*trg 5

实际上,您不能将 web.config 用于 403 重定向。

您可以做的是覆盖OnActionExecuted控制器以检查状态代码并重定向到 web.config 中定义的任何内容,如下所示

网页配置:

<customErrors mode="On">
  <error statusCode="403" redirect="~/Error/Forbidden" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

你的家庭控制器

public class HomeController : Controller
{
    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.HttpContext.Response.StatusCode == 403)
        {
            var config = (CustomErrorsSection)
                           WebConfigurationManager.GetSection("system.web/customErrors");
            string urlToRedirectTo = config.Errors["403"].Redirect;
            filterContext.Result = Redirect(urlToRedirectTo);
        }
        base.OnActionExecuted(filterContext);
    }

    public ActionResult Edit(int idObject)
    {
         if(!user.OnwsObject(idObject))
         {
             Response.StatusCode = 403;
         }

         return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

错误控制器:

public class ErrorController : Controller
{
    public ActionResult Forbidden()
    {
        Response.StatusCode = 403;
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

更通用的解决方案是创建一个动作过滤器,您可以将其应用于控制器或单个动作:

public class HandleForbiddenRedirect : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.HttpContext.Response.StatusCode == 403)
        {
            var config = (CustomErrorsSection)
                           WebConfigurationManager.GetSection("system.web/customErrors");
            string urlToRedirectTo = config.Errors["403"].Redirect;
            filterContext.Result = new RedirectResult(urlToRedirectTo);
        }
        base.OnActionExecuted(filterContext);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以将动作过滤器应用于控制器,以便所有动作都重定向到 403

[HandleForbiddenRedirect]
public class HomeController : Controller
{
    //...
}
Run Code Online (Sandbox Code Playgroud)

或者在 403 上有一个单独的动作重定向

public class HomeController : Controller
{
    [HandleForbiddenRedirect]
    public ActionResult Edit(int idObject)
    {
         //...
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您不想装饰所有控制器和动作,而是想将其应用到任何地方,您可以将其添加到 Global.asax 的 Application_Start 的过滤器集合中

GlobalFilters.Filters.Add(new HandleForbiddenRedirect());
Run Code Online (Sandbox Code Playgroud)


Bra*_*o11 0

请参阅这篇文章,这应该可以解决您的问题。

如何使自定义错误页面在 ASP.NET MVC 4 中工作

基本上你需要使用 viewresult 创建控制器或

ASP.NET MVC 处理错误