• C# MVC模式 404 500页面设置方法


    <customErrors mode="On" defaultRedirect="Controllers/Action">
    <error statusCode="403" redirect="Controllers/Action" /> <error statusCode="404" redirect="Controllers/Action" /> </customErrors> 这里不是对应你想转到的页面而是你所想跳转的某个Controllers 中的某个Action

    方法二:

    复制代码
    protected void Application_EndRequest()
    {
            var statusCode = Context.Response.StatusCode;
            var routingData = Context.Request.RequestContext.RouteData;
            if (statusCode == 404 || statusCode == 500)
            {
               Response.Clear();
               var area = DataHelper.ConvertTo(routingData.DataTokens["area"], string.Empty);
               if (area == "Admin")
               {
                    Response.RedirectToRoute("Admin_Default", new { controller = "BackError", action = "NotFound", IsReload = 1 });
               }
               else
               {
                    Response.RedirectToRoute("Default", new { controller = "Error", action = "NotFound", id = UrlParameter.Optional });
               }
    
           }
    }
    复制代码

    方法三:

    Global.aspx.cs
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)  
    {  
        filters.Add(new CustomHandlerErrorAttribute());  
    }  

    CustomHandlerErrorAttribute.cs

    复制代码
    public class CustomHandlerErrorAttribute : HandleErrorAttribute  
    {  
        public override void OnException(ExceptionContext filterContext)  
        {  
            if (filterContext.ExceptionHandled)  
            {  
                return;  
            }  
      
            filterContext.Controller.ViewData.Model = filterContext.Exception;  
      
            filterContext.Result = new ViewResult   
            {   
                ViewName = "Error",   
                ViewData = filterContext.Controller.ViewData   
            };  
      
            filterContext.ExceptionHandled = true;  
        }  
    }  
    复制代码

    web.config <system.web>

    <customErrors mode="On">  
      <error redirect="/home/error" statusCode="404" />  
    </customErrors>  

    web.config  <system.webServer>

    <httpErrors errorMode="Custom" existingResponse="PassThrough">  
    </httpErrors>  

    Error.cshtml

    复制代码
    <div class="box">  
        @{  
            
            var exception = ViewData.Model;  
            var statusCode = exception == null ? 404 : 500;  
            Response.StatusCode = statusCode;  
            if (statusCode == 404)  
            {  
                <h3>404 Page not found!</h3>  
                <p>没有找到该网页!</p>  
            }  
            else if (statusCode == 500)  
            {  
                <h3>500 程序异常</h3>  
                <p>@exception.Message</p>  
            }  
        }  
        <p style="font-size: 12px; color: Gray">请使用浏览器的后退功能已保证您填写的数据没有丢失!</p>  
    </div>  
    复制代码
  • 相关阅读:
    Java内存回收
    Android四大基本组件介绍与生命周期
    JAVA中获取当前系统时间
    jquery如何判断元素是否被点击_百度知道
    css控制div显示/隐藏方法及2种方法比较原码
    点击图标不断震动效果
    jquery 如何动态添加、删除class样式方法介绍_jquery_脚本之家
    HTML5绘制矩形和圆形并且还有获取在这个图层内的坐标的思路和代码
    Attribute name invalid for tag form according to TLD异常解决办法_gaigai_百度空间
    html5绘图
  • 原文地址:https://www.cnblogs.com/webenh/p/6086257.html
Copyright © 2020-2023  润新知