Sharepoint custom error page

Source: Internet
Author: User

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 putApplication_ErrorInto youGlobal. asaxFile. However in SharePoint that place has been taken by the product itself So if you want to do customized approach then you can takeHttpModuleApproach 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:

 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");  }} 

 

You can probably see from the code that I'll attach my code toErrorEvent and in my event I'll do some basic stuff and then transfer to myMyCustomErrorPage. aspx.I usedServer. TransferJust because I want user to stay at the same url where exception happened. If I wocould useResponse. RedirectIt wocould "change" the url at the users browser. Same "change" wocould happen if your custom error page wocould 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 yourIHttpModuleBefore SharePoint specific les in yourWeb. configOr otherwise your error routines may not work as you wowould perform CT. Here's example from that:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><configuration> <!-- ... -->  

 

 

See line 6 where I put my exception handler definition.

Anyways... Happy hacking!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.