Catching unhandled exceptions in SharePoint--Janne Mattila
2008-09-24 22:14
211 查看
http://blogs.msdn.com/jannemattila/archive/2008/02/04/catching-unhandled-exceptions-in-sharepoint.aspx
If you have done some dev stuff with MOSS you have most likely seen this:
"An unexpected error has occurred. " is something that you probably don't want to see at your browser.... you want to have customized error page. In ASP.NET application you normally put Application_Error into you global.asax file. However in SharePoint that place has been taken by the product itself :-) So if you want to do customized approach then you can take HttpModule approach which I'm going to go through in this post.
So let's create our custom exception handler http module. Here's the code for that:
You can probably see from the code that I'll attach my code to the Error event and in my event I'll do some basic stuff and then transfer to my MyCustomErrorPage.aspx. I used Server.Transfer just because I want user to stay at the same url where exception happened. If I would use Response.Redirect it would "change" the url at the users browser. Same "change" would happen if your custom error page would be normal SharePoint publishing page (i.e. /Pages/MyCustomErrorPage.aspx). If the url stays the same the user can actually press F5 and retry the operation right away. Of course it can be bad thing too and you may want to redirect to another page to avoid creating the same exception all over again. I'll let you decide what you want :-) So do some testing and then decide what's good for you.
But one important thing to notice. You need to put your IHttpModule before SharePoint specific modules in your web.config or otherwise your error routines may not work as you would expect. Here's example from that:
See line 6 where I put my exception handler definition.
Anyways... Happy hacking!
J
If you have done some dev stuff with MOSS you have most likely seen this:
"An unexpected error has occurred. " is something that you probably don't want to see at your browser.... you want to have customized error page. In ASP.NET application you normally put Application_Error into you global.asax file. However in SharePoint that place has been taken by the product itself :-) So if you want to do customized approach then you can take HttpModule approach which I'm going to go through in this post.
So let's create our custom exception handler http module. Here's the code for that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | using System; using System.Web; public class MyExceptionHandler : IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.Error += new EventHandler(context_Error); } void context_Error(object sender, EventArgs e) { Exception[] unhandledExceptions = HttpContext.Current.AllErrors; foreach (Exception ex in unhandledExceptions) { // TODO: log your errors } HttpContext.Current.Server.ClearError(); HttpContext.Current.Response.Clear(); HttpContext.Current.Server.Transfer("/_layouts/MyCustomErrorPage.aspx"); } } |
But one important thing to notice. You need to put your IHttpModule before SharePoint specific modules in your web.config or otherwise your error routines may not work as you would expect. Here's example from that:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <configuration> <!-- ... --> <httpModules> <clear /> <add name="MyExceptionHandler" type="MyExceptionHandler,Example, Version=1.0.0.0, Culture=neutral,PublicKeyToken=34a2bd01f6f6eb10" /> <add name="SPRequest" type="Microsoft.SharePoint.ApplicationRuntime.SPRequestModule, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" /> <add name="OutputCache" type="System.Web.Caching.OutputCacheModule" /> <!-- ... --> </configuration |
Anyways... Happy hacking!
J
相关文章推荐
- SharePoint Add-in Model (App Model) 介绍 – 概念、托管方式、开发语言
- Get the SharePoint groups in Infopath 2010 and SharePoint 2010 by current user without any code
- Building Custom Field Types in Windows SharePoint Services 3.0
- Switch view in the InfoPath form according to the whether current user is in SharePoint group admin
- How to copy files between sites using JavaScript REST in Office365 / SharePoint 2013
- Configuring FBA in SharePoint 2010
- Search List Items in SharePoint Portal Server
- Remote Debugging GAC'd Assemblies in SharePoint
- How to Configure Workflow Infrastructure 2013 in Sharepoint 2013
- how to find sharepoint app pool in win server2008?
- 记录一个:Create, Update, Delete and Copy Item Activity in SharePoint 2010 Workflow
- Setup SSRS in SharePoint Integrated Mode
- Changes to the Design View in SharePoint Designer 2013
- 好文推荐:Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0
- Change language of UI using custom control in SharePoint 2010
- How to Enable or Disable Document Property Promotion (ParserEnabled) in SharePoint 2013
- How to Add onLoad JavaScript event in SharePoint?
- Post/Reply a post by Social feed REST API in SharePoint 2013
- 使用npm+babel+webpack+React搭建SharePoint hosted add-in开发环境
- Upgrade custom workflow in SharePoint