当某个异常发生时,如何确保我的MVC应用程序重定向到一个错误页面?

时间:2023-01-13 21:52:02

I have seen a nice example whereby the MVC controller class inherits from another controller class which handles an exception and returns a view with the error inside like so:

我看到过一个很好的例子,MVC控制器类继承了另一个控制器类,它处理一个异常并返回一个视图,其中包含错误如下:

public class ApplicationController : Controller
{
    public ActionResult NotFound()
    {
        return View("NotFound");
    }

    public ActionResult InsufficientPriveleges()
    {
        return View("InsufficientPriveleges");
    }

    protected override void OnException(ExceptionContext filterContext)
    {
        if (filterContext.Exception is NotFoundException)
        {
            filterContext.Result = NotFound();
            filterContext.ExceptionHandled = true;
            return;
        }
        if (filterContext.Exception is InsufficientPrivelegesException)
        {
            filterContext.Result = InsufficientPriveleges();
            filterContext.ExceptionHandled = true;
            return;
        }

        base.OnException(filterContext);
    }
}

However, I've noticed that say for example my controller is loading in a partial view for something the user should not have access to, the error will then be shown inside the partial view on the page.

但是,我注意到,例如,我的控制器正在为用户不应该访问的内容加载部分视图,错误将显示在页面的部分视图中。

I'd like the whole page to show the error, in effect the exception should redirect to a completely new page. How can I achieve this using the current class shown above?

我希望整个页面显示错误,实际上,异常应该重定向到一个全新的页面。如何使用上面显示的当前类来实现这一点?

2 个解决方案

#1


2  

You can create a mapping from HTTP result codes to specific error handlers in your web.config:

可以在web.config中创建从HTTP结果代码到特定错误处理程序的映射:

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

and then create a custom error controller

然后创建一个自定义错误控制器

public class ErrorController
{ 
        public ActionResult Index ()
        {
            return View ("Error");
        }

        public ActionResult Unauthorized ()
        {
            return View ("Error401");
        }

        public ActionResult NotFound ()
        {
            return View ("Error404");
    }
    }

and then throw an HttpException from your controllers

然后从控制器中抛出一个HttpException

public ActionResult Test ()
{
    throw new HttpException ((int)HttpStatusCode.Unauthorized, "Unauthorized");
}

another alternative is to use a custom FilterAttribute to check permissions which throws a HttpException

另一种选择是使用自定义FilterAttribute来检查引发HttpException的权限

[Authorize (Roles = SiteRoles.Admin)]
public ActionResult Test ()
{
  return View ();
}

#2


0  

You gotta handle ajax call differently. I would simply use error() of jquery $.ajax to handle that kind of ajax call error.

你必须以不同的方式处理ajax调用。我只使用jquery $的错误()。ajax处理这种ajax调用错误。

$.ajax({
    ...
    success: function(g) {
          //process normal
    },
    error: function(request, status, error) {
          //process exception here
    }
})

#1


2  

You can create a mapping from HTTP result codes to specific error handlers in your web.config:

可以在web.config中创建从HTTP结果代码到特定错误处理程序的映射:

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

and then create a custom error controller

然后创建一个自定义错误控制器

public class ErrorController
{ 
        public ActionResult Index ()
        {
            return View ("Error");
        }

        public ActionResult Unauthorized ()
        {
            return View ("Error401");
        }

        public ActionResult NotFound ()
        {
            return View ("Error404");
    }
    }

and then throw an HttpException from your controllers

然后从控制器中抛出一个HttpException

public ActionResult Test ()
{
    throw new HttpException ((int)HttpStatusCode.Unauthorized, "Unauthorized");
}

another alternative is to use a custom FilterAttribute to check permissions which throws a HttpException

另一种选择是使用自定义FilterAttribute来检查引发HttpException的权限

[Authorize (Roles = SiteRoles.Admin)]
public ActionResult Test ()
{
  return View ();
}

#2


0  

You gotta handle ajax call differently. I would simply use error() of jquery $.ajax to handle that kind of ajax call error.

你必须以不同的方式处理ajax调用。我只使用jquery $的错误()。ajax处理这种ajax调用错误。

$.ajax({
    ...
    success: function(g) {
          //process normal
    },
    error: function(request, status, error) {
          //process exception here
    }
})