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

推荐订阅源

博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cisco Blogs
V
Vulnerabilities – Threatpost
G
GRAHAM CLULEY
N
News | PayPal Newsroom
NISL@THU
NISL@THU
雷峰网
雷峰网
J
Java Code Geeks
Latest news
Latest news
aimingoo的专栏
aimingoo的专栏
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cisco Talos Blog
Cisco Talos Blog
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
Spread Privacy
Spread Privacy
V2EX - 技术
V2EX - 技术
S
Schneier on Security
K
Kaspersky official blog
Recent Announcements
Recent Announcements
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
S
SegmentFault 最新的问题
Security Archives - TechRepublic
Security Archives - TechRepublic
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
Scott Helme
Scott Helme
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
量子位
A
Arctic Wolf

博客园 - 君之蘭

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)  评论()    收藏  举报