zoukankan      html  css  js  c++  java
  • asp.net mvc4中自定义404页面

    原文地址:http://www.chuchur.com/asp-net-mvc4-404/

    定义404 方法当然有很多种。不同的方法所展现的形式也不一样,用户所体验也不一样。以下提供2两种

    方法一:

    1. 在web.config 中找到节点<system.web>中启用404配置

    <customErrors defaultRedirect="~/Error" mode="On" redirectMode="ResponseRedirect">
    <error redirect="/Error" statusCode="404" />
    </customErrors>

    2.定义一个 controllers Error(这个随你) ,在action中如下定义

    public ActionResult Index()
    {
        Response.Status = "404 Not Found";
        Response.StatusCode = 404;
        return View();
    }

    这种方式 默认为给你的url加上 ?aspxerrorpath=/ eg:http://localhost/Error??aspxerrorpath=/123456 故不推荐试用

    方法二:

    打开 Global.asax 文件 定义错误转向地址(controller/action)

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
        {
            Response.Redirect("/Error");
        }
    }
    
    

    注意事项:在开发时候,我们经常会在 Global.asax 中的 Application_Error 方法中使用 Response.Redirect 方法跳转到自定义错误页,但有时候(特别是当站点部署到IIS后)Application_Error 方法中使用 Response.Redirect 方法会失效,当发生异常错误后还是显示的默认错误黄页。其根本原因是尽管我们在 Application_Error 方法中使用了 Response.Redirect 方法,但是当系统发生异常错误后 Asp.Net 认为异常并没有被处理,所以不会跳转到 Application_Error 方法中 Response.Redirect 指向的页面,最终还是会跳转到默认错误黄页。解决这个问题的办法很简单就是在 Application_Error 方法中使用 Response.Redirect 做跳转前,先调用 Server.ClearError() 方法告诉系统发生的异常错误已经被处理了,这样再调用 Response.Redirect 方法系统就会跳转到自定义错误页面了。 

  • 相关阅读:
    ObjectC----NSObject常用方法和OC反射机制
    自定义 URL Scheme 完全指南
    一文让你彻底了解iOS字体相关知识
    Objective-C Category 的实现原理[转]
    Objective-C Autorelease Pool 的实现原理[转]
    eclipse无法连接到makertplace
    logstash日志分析的配置和使用
    在CentOS或RHEL防火墙上开启端口
    redhat7下配置tomcat7,更改端口8080后无法访问页面
    Uiautomatorviewer报错:Unexpected error while obtaining UI hierarchy java.lang.reflect.InvocationTargetException
  • 原文地址:https://www.cnblogs.com/jiyang2008/p/4948130.html
Copyright © 2011-2022 走看看