zoukankan      html  css  js  c++  java
  • .net mvc 自定义错误页面

    1、Global.asax.cs中,加入如下代码

    protected void Application_Error(Object sender, EventArgs e)
            {
                Exception exception = Server.GetLastError();
                if (exception != null)
                {
                    HttpException httpException = exception as HttpException;
                    if (httpException != null)
                    {
                        int errorCode = httpException.GetHttpCode();
                        if (errorCode == 400 || errorCode == 404)
                        {
                            Response.StatusCode = 404;
                            Response.Redirect(string.Format("~/ErrorPage/Index/404"), true);
                            Server.ClearError();
                            return;
                        }
                    }
    
                    var postData = string.Empty;
                    try
                    {
                        using (System.IO.Stream stream = Request.InputStream)
                        {
                            using (System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8))
                            {
                                postData = streamReader.ReadToEnd();
                            }
                        }
                    }
                    catch { }
    
                    //此处可写日志
    
                    Response.StatusCode = 500;
                    Response.Redirect(string.Format("~/ErrorPage/Index/500"), true);
                    Server.ClearError();
                }
            }
    

      2、新建控制器

    public class ErrorPageController : Controller
        {
            //
            // GET: /ErrorPage/
    
            public ActionResult Index(int id)
            {
                ViewBag.ErrorCode = id;
                
                return View();
            }
    
        }
    

      3、路由要有如下的路由,保证能正确找到方法,只要保证能正确找到方法,路由并无限制

    routes.MapRoute(
                    name: "Default6",
                    url: "{controller}/{action}/{id}",
                    defaults: new { culture = cul, controller = "Home", action = "Index", id = UrlParameter.Optional },
                    namespaces: new string[] { "AiAn.GPS.Web.Controllers" }
                );
    

      4、根目录下的web.config 设置customError为off 一般默认为off

    <customErrors mode="Off">
        </customErrors>
    

      5、注释filterConfig.cs中的系统错误处理代码

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    		{
                //filters.Add(new HandleErrorAttribute());
            }
    

      

  • 相关阅读:
    js数组去重五种方法
    wm_concat 多行字符串拼接
    ORACLE WITH AS 简单用法
    layui laytpl 语法
    看懂Oracle执行计划
    GIT RM -R --CACHED 去掉已经托管在GIT上的文件
    sourceTree使用教程--拉取、获取
    SourceTree忽略文件和文件夹
    layui table 详细讲解
    利用POI实现下拉框级联
  • 原文地址:https://www.cnblogs.com/zhoushangwu/p/11188777.html
Copyright © 2011-2022 走看看