Asp.net MVC 自定义错误页面以及return HttpNotFound遇到的问题
今天在处理mvc 项目404和500页面时,发现我以前比较喜欢用的Return HttpNotFound()没有跳转到我在webconfig中配置的自定义404页面,而且也不会去执行Global中的Application_Error方法,经过一番查阅资料,发现这个问题得去想别的办法去做,具体的做法有三种,如下:
1.放弃Return HttpNotFound(),适用throw new HttpException(404, "page not found");
2.让所有的Controller继承自BaseController,然后BaseController重写HttpNotFound方法,代码如下:
protected override HttpNotFoundResult HttpNotFound(string statusDescription)
{
this.Response.StatusCode = ;
this.Response.TrySkipIisCustomErrors = true;
Response.Clear();
Response.Redirect("~/FileNotFound.html");
Response.End();
return null;
}
3.使用Fliter来处理异常,代码如下;
public class CustomViewForHttpStatusResultFilter : IResultFilter, IExceptionFilter
{
string viewName;
int statusCode; #region Ctor
public CustomViewForHttpStatusResultFilter(HttpStatusCodeResult prototype, string viewName)
: this(prototype.StatusCode, viewName)
{
} public CustomViewForHttpStatusResultFilter(int statusCode, string viewName)
{
this.viewName = viewName;
this.statusCode = statusCode;
}
#endregion public void OnResultExecuted(ResultExecutedContext filterContext)
{
HttpStatusCodeResult httpStatusCodeResult = filterContext.Result as HttpStatusCodeResult;
//比较重要的一句,当请求完成后,发现
if (httpStatusCodeResult != null && httpStatusCodeResult.StatusCode == statusCode)
{
ExecuteCustomViewResult(filterContext.Controller.ControllerContext); }
} public void OnResultExecuting(ResultExecutingContext filterContext)
{
} public void OnException(ExceptionContext filterContext)
{
HttpException httpException = filterContext.Exception as HttpException; if (httpException != null && httpException.GetHttpCode() == statusCode)
{
ExecuteCustomViewResult(filterContext.Controller.ControllerContext);
// This causes ELMAH not to log exceptions, so commented out
//filterContext.ExceptionHandled = true;
}
} void ExecuteCustomViewResult(ControllerContext controllerContext)
{
ViewResult viewResult = new ViewResult();
viewResult.ViewName = viewName;
viewResult.ViewData = controllerContext.Controller.ViewData;
viewResult.TempData = controllerContext.Controller.TempData;
viewResult.ExecuteResult(controllerContext);
controllerContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
}
Asp.net MVC 自定义错误页面以及return HttpNotFound遇到的问题的更多相关文章
- ASP.NET MVC 自定义错误页面心得
自定义错误页面的目的,就是为了能让程序在出现错误/异常的时候,能够有较好的显示体验. 所以,首先要先了解,我们可以在哪里捕获异常. 当程序发生错误的时候,我们可以在两个地方捕获: Global里面的A ...
- 在ASP.NET MVC自定义错误页面
异常处理跳转页面 第一步,在项目的Web.config文件中找到节点<system.web> 在此节点下添加配置(Error为定义的控制器也可以多添加些error标签用于区分不同的错误) ...
- MVC自定义错误页面
MVC异常处理主要有三种方案:1.基于HandleErrorAttribute重写OnException方法:2.基于Global.apsx添加Application_Error方法:3.直接在Web ...
- MVC 自定义 错误页面
很多时候,我们需要自定义错误页面,用来当发生异常后引导用户进入一个比较友好的错误页面. 在这里,我归结一下我常用的2个方案 1 通过Global.asax 文件来处理异常信息(这个不管是 MVC ...
- ASP.net MVC自定义错误处理页面的方法
在ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那 ...
- Spring MVC自定义错误页面
在web.xml中添加: <error-page(其他属性404...省略咯)> <location>/error</location> </error-pa ...
- .net mvc 自定义错误页面
1.Global.asax.cs中,加入如下代码 protected void Application_Error(Object sender, EventArgs e) { Exception ex ...
- ASP.NETMVC自定义错误页面真的简单吗?
Note:文章前半部分翻译自 http://benfoster.io/blog/aspnet-mvc-custom-error-pages ,着急的可直接看总结~ 如果你在设置asp.net mvc自 ...
- ASP.NET网站中设置404自定义错误页面
在用ASP.NET WebForm开发一个网站时,需要自定义404错误页面. 做法是这样的 在网站根目录下建立了一个404.html的错误页面,然后在Global.asax文件中,加入如下代码: &l ...
随机推荐
- 九 ServerSocketChannel
ServerSocketChannel是一个可以监听进来的TCP连接的通道,就像标准IO的ServerSocket一样.ServerSocketChannel类在java.nio.channels包中 ...
- maven filter不起作用
遇到的一个坑, spring boot + maven maven fileter没有起作用.spring boot把默认占位符改了 参考:https://blog.csdn.net/mn960mn/ ...
- Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)
最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对“基数”以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了“HyperLogLog”,从而引出了Card ...
- 原生JS编写getByClass、addClass、removeClass、hasClass
前言: 年后换了工作,在现在的公司写交互主要使用JS原生:刚刚入门前端的时候写交互一直用的原生JS,虽然用的不怎么样.后来去之前的公司之后,leader主张把jQuery用好,JS原生自然就熟练了:一 ...
- Csharp: speech to text, text to speech in win
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 移动web开发ajax缓存操作
移动web开发过程中网速是必须考虑的一个因素,所以一般是尽可能的在本地存储数据,避免弱网环境下请求数据失败导致页面没有内容的情况. 前后端分离是web开发的必然趋势,在PC端我们有时甚至为了避免aja ...
- <Android 基础(十九)> CoordinatorLayout
介绍 CoordinatorLayout,中文翻译,协调布局,顾名思义,此布局中的子View之间,子View与父布局之间应该是可以协调工作的,如何协调,Behavior. 今天看下Android St ...
- UNIX/Linux系统管理技术手册(2)----bash脚本编程
1. 一个简单的例子: $ vim readname.sh #file:readname.sh#!/bin/bash echo -n "Enter your name: " rea ...
- OpenfileDialog选择照片的简单应用
OpenFileDialog openFileDlg = new OpenFileDialog(); openFileDlg.Title = "选择文件"; openFileDlg ...
- 形象解释C#、Net、Asp.net
下文是写给计算机小白的,尽量用形象的语言来让她们明白这些比较抽象的概念. -------------------------------------- C#: 你和美国人说话要说英语 和中国人说话说汉 ...