<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>

C# MVC模式 404 500页面设置方法的更多相关文章

  1. spring MVC模式拦截所有入口方法的入参出参打印

    import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; im ...

  2. 2016/5/6 thinkphp ①框架 ② 框架项目部署 ③MVC模式 ④控制器访问及路由解析 ⑤开发和生产模式 ⑥控制器和对应方法创建 ⑦视图模板文件创建 ⑧url地址大小写设置 ⑨空操作空控制器 ⑩项目分组

    真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难,代码风格不一样) 项目稳 ...

  3. Spring MVC自定义403,404,500状态码返回页面

    代码 HTTP状态码干货:http://tool.oschina.net/commons?type=5 import org.springframework.boot.web.servlet.erro ...

  4. MVC模式设计的Web层框架初识

    struts是个什么东西? struts是一个按MVC模式设计的Web层框架,其实它就是一个大大的servlet,这个Servlet名为ActionServlet,或是ActionServlet的子类 ...

  5. Windows Server 2008 R2 IIS7.5 部署 MVC HTTP 404.0 Not Found 错误

    如图 在Windows Server 2008 R2 IIS7.5 部署 MVC HTTP 404.0 Not Found 错误,在Win7环境下测试正常,在百度中查找相关解决方法,如修改配置文件等, ...

  6. ASP.Net MVC开发基础学习笔记:一、走向MVC模式

    一.ASP.Net的两种开发模式 1.1 ASP.Net WebForm的开发模式 (1)处理流程 在传统的WebForm模式下,我们请求一个例如http://www.aspnetmvc.com/bl ...

  7. [ASP.NET MVC 小牛之路]01 - 理解MVC模式

    本人博客已转移至:http://www.exblr.com/liam  PS:MVC出来很久了,工作上一直没机会用.出于兴趣,工作之余我将展开对MVC的深入学习,通过博文来记录所学所得,并希望能得到各 ...

  8. 深入理解MVC模式

    一,什么是MVC模式 该模式是一种软件设计典范,他把软件系统划分为三个基本部分:模型层(Model).视图层(View).控制器(Controller) *Model(模型)表示应用程序核心(比如数据 ...

  9. MVC模式与Android

    MVC模式是软件工程中的一种软件架构,“Model-View-Controller”的缩写,中文翻译为“模型-视图-控制器”. MVC模式将一个交互式应用程序分为3各组件: 1.Model(模型):业 ...

随机推荐

  1. Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive.

    调试网站时,异常出现:Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive. S ...

  2. Socket开发框架之数据加密及完整性检查

    在前面两篇介绍了Socket框架的设计思路以及数据传输方面的内容,整个框架的设计指导原则就是易于使用及安全性较好,可以用来从客户端到服务端的数据安全传输,那么实现这个目标就需要设计好消息的传输和数据加 ...

  3. Asp.Net WebForm和MVC同样优秀!

    不是说MVC不好,而是WebForm并没有那么不堪,和Mvc同样优秀.对说WebForm缺点多的,表示不服,求指教,虽然本人有些见解可能比较浅薄. 看过很多文章和书籍,都会拿MVC模式和WebForm ...

  4. android 开发环境搭建

    http://www.cnblogs.com/bjzhanghao/archive/2012/11/14/2769409.html http://jingyan.baidu.com/article/7 ...

  5. 图的存储及遍历 深度遍历和广度遍历 C++代码实现

    /*图的存储及遍历*/ #include<iostream> using namespace std; //----------------------------------- //邻接 ...

  6. C#的变迁史 - C# 5.0 之其他增强篇

    1. 内置zip压缩与解压 Zip是最为常用的文件压缩格式之一,也被几乎所有操作系统支持.在之前,使用程序去进行zip压缩和解压要靠第三方组件去支持,这一点在.NET4.5中已有所改观,Zip压缩和解 ...

  7. MVC之前的那点事儿系列(1):进入CLR

    MVC之前的那点事儿系列,是笔者在2012年初阅读MVC3源码的时候整理的,主要讲述的是从HTTP请求道进入MVCHandler之前的内容,包括了原创,翻译,转载,整理等各类型文章,当然也参考了博客园 ...

  8. Winform 导入导出方法

    导出时:引用 using Excel = Microsoft.Office.Interop.Excel; #region 读取excel //打开方法 public DataTable Excelto ...

  9. LeetCode3:Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  10. 设置MySQL服务自动运行

    一般情况下,MySQL安装以后是自动运行的,不知道我这台机器是什么原因,MySQL不能自动运行,每次开机后都要手动运行mysqld.exe,比较麻烦,于是用以下方法将MySQL自动启动: 1. 运行c ...