MVC后台异常跳转页面

59 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;

namespace MvcApplication1
{
    /// <summary>
    /// 自定义404异常
    /// </summary>
    public class NotFoundException : Exception
    {}
    /// <summary>
    /// 自定义500异常
    /// </summary>
    public class ServerException : Exception { }


    public class CustomerizedErrorHandledAttribute: Attribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            HttpResponseBase resp = filterContext.RequestContext.HttpContext.Response;

            //指定要跳转的路径页(必须全部在Shared下)
            ViewResult vr = null;

            if (filterContext.Exception is NotFoundException)
            {
                resp.StatusCode = 404;
                vr = new ViewResult { ViewName = "404Error" };
            }
            else if (filterContext.Exception is ServerException)
            {
                resp.StatusCode = 500;
                vr = new ViewResult { ViewName = "500Error" };
            }
            filterContext.ExceptionHandled = true;
            filterContext.Result = vr;
        }
    }
}




protected void Application_EndRequest()
 2   {
 3         var statusCode = Context.Response.StatusCode;
 4         var routingData = Context.Request.RequestContext.RouteData;
 5         if (statusCode == 404 || statusCode == 500)
 6         {
 7            Response.Clear();
 8            var area = DataHelper.ConvertTo(routingData.DataTokens["area"], string.Empty);
 9            if (area == "Admin")
10            {
11                 Response.RedirectToRoute("Admin_Default", new { controller = "BackError", action = "NotFound", IsReload = 1 });
12            }
13            else
14            {
15                 Response.RedirectToRoute("Default", new { controller = "Error", action = "NotFound", id = UrlParameter.Optional });
16            }
17 
18        }
19 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值