开发者

different error page for Partial View

开发者 https://www.devze.com 2023-04-01 09:35 出处:网络
I have a Error page with all Layout and Header, which works fine when error occured on the main View, it shows the error page correctly.

I have a Error page with all Layout and Header, which works fine when error occured on the main View, it shows the error page correctly. But wh开发者_如何学编程en any error occured while rendering any partial view, it breaks the whole UI, because the error page also have Header,

So i wanted to know inside Global.asax (application_Error) that if the request is for partial view redirect it to PartialError page else redirect it to Fullerror page.

Please let me know how can i achieve that. thanks.


I suggest using filter attribute. You can implement something like this:

public class RedirectOnErrorAttribute : FilterAttribute, IExceptionFilter {

bool IsPartialRequest = false;

public void OnException(ExceptionContext filterContext) {
        if(filterContext.ExceptionHandled) return;
        /*then you can redirect to a specific page or to, for example,
special error handling controller*/
         filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Error", action = "HandleError", isPartialError = IsPartialRequest }));
         filterContext.ExceptionHandled = true;
         filterContext.HttpContext.Response.Clear();
}
}

And in error handling controller in HandleError action method you can return specific View depending on the isPartialError parameter. BTW, you can do another useful things in this action method - for example log error info somehow.

To use this attribute you can decorate your controller classes with it:

[RedirectOnError]
public class MyController : Controller {

  public ViewResult Index () {}
  public ActionResult Create() {}

  [RedirectOnError(IsPartialRequest=true)]
  public PartialViewResult ListCategories() {}

}

pay attention to the attribute usage with ListCategories method - I explicitly told that this is the partial request. The reason I did it is simple - routing system knows nothing about the fact that the result will be used as partial.

0

精彩评论

暂无评论...
验证码 换一张
取 消