惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Schneier on Security
Schneier on Security
C
Cisco Blogs
Help Net Security
Help Net Security
I
Intezer
Simon Willison's Weblog
Simon Willison's Weblog
Know Your Adversary
Know Your Adversary
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
小众软件
小众软件
Jina AI
Jina AI
量子位
T
Threatpost
Forbes - Security
Forbes - Security
L
LINUX DO - 最新话题
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cyber Attacks, Cyber Crime and Cyber Security
有赞技术团队
有赞技术团队
博客园_首页
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Scott Helme
Scott Helme
博客园 - 【当耐特】
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Last Watchdog
The Last Watchdog
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
SecWiki News
SecWiki News
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
The Cloudflare Blog
S
Schneier on Security
爱范儿
爱范儿
N
News and Events Feed by Topic
NISL@THU
NISL@THU
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 君之蘭

WPF打印票据 easyui plugin——etreegrid:CRUD Treegrid 【经验谈】XmlSerializer的坑 从抽象谈起(三):AOP编程和ASP.NET MVC 从抽象谈起(二):观察者模式与回调 从抽象谈起(一):工厂模式与策略模式 SQL SERVER BI 入门:(2) Analysis Service 应用 SQL SERVER BI 入门:(1)安装与基础概念 HTML5 Canvas编写五彩连珠(6):试玩 HTML5 Canvas编写五彩连珠(5):寻路 HTML5 Canvas编写五彩连珠(4):动画 HTML5 Canvas编写五彩连珠(3):设计 HTML5 Canvas编写五彩连珠(2):画图 HTML5 Canvas编写五彩连珠(1):预览 Trie树-脏词过滤应用 带你走进缓存世界(6):共享缓存 ASP.NET MVC3 Custom FormAuthorize 带你走进缓存世界(5):一显身手 带你走进缓存世界(4):缓存之缓
ASP.NET MVC3 Custom ErrorPages 500/404
君之蘭 · 2011-10-08 · via 博客园 - 君之蘭

Global.aspx.cs

  1. public static void RegisterGlobalFilters(GlobalFilterCollection filters)  
  2. {  
  3.     filters.Add(new CustomHandlerErrorAttribute());  
  4. }  


CustomHandlerErrorAttribute.cs

  1. public class CustomHandlerErrorAttribute : HandleErrorAttribute  
  2. {  
  3.     public override void OnException(ExceptionContext filterContext)  
  4.     {  
  5.         if (filterContext.ExceptionHandled)  
  6.         {  
  7.             return;  
  8.         }  
  9.   
  10.         filterContext.Controller.ViewData.Model = filterContext.Exception;  
  11.   
  12.         filterContext.Result = new ViewResult   
  13.         {   
  14.             ViewName = "Error",   
  15.             ViewData = filterContext.Controller.ViewData   
  16.         };  
  17.   
  18.         filterContext.ExceptionHandled = true;  
  19.     }  
  20. }  


web.config <system.web>

  1. <customErrors mode="On">  
  2.   <error redirect="/home/error" statusCode="404" />  
  3. </customErrors>  

web.config  <system.webServer>

  1. <httpErrors errorMode="Custom" existingResponse="PassThrough">  
  2. </httpErrors>  

Error.cshtml

  1. <div class="box">  
  2.     @{  
  3.         
  4.         var exception = ViewData.Model;  
  5.         var statusCode = exception == null ? 404 : 500;  
  6.         Response.StatusCode = statusCode;  
  7.         if (statusCode == 404)  
  8.         {  
  9.             <h3>404 Page not found!</h3>  
  10.             <p>没有找到该网页!</p>  
  11.         }  
  12.         else if (statusCode == 500)  
  13.         {  
  14.             <h3>500 程序异常</h3>  
  15.             <p>@exception.Message</p>  
  16.         }  
  17.     }  
  18.     <p style="font-size: 12px; color: Gray">请使用浏览器的后退功能已保证您填写的数据没有丢失!</p>  
  19. </div>  

posted @ 2011-10-08 11:42  君之蘭  阅读(1508)  评论()    收藏  举报