Question

Is there a way to put an error handler HTTPModule at the beginning of the modules section in web.config without doing this manually?

I've tried the supplemental web.config approach like this:

  <add path="configuration/system.webServer/modules" id="{125A654F-9220-42F0-A97A-1746252468DE}">
    <add name="CustomErrorHandler" type="com.handlers.CustomErrorHandler, Assemblyname, Version=4.5.6.7, Culture=neutral, PublicKeyToken=3450345789" />
  </add>

The problem is that the custom error handler has to be placed before the standard handler. If you don't, sharepoint handles the exception itself and delivers the standard exception page.

Expected:

 <modules runAllManagedModulesForAllRequests="true">
  <add name="CustomErrorHandler" type="com.handlers.CustomErrorHandler, Assemblyname, Version=4.5.6.7, Culture=neutral, PublicKeyToken=3450345789" />
  <remove name="FileAuthorization" />
  <remove name="Profile" />
  <remove name="WebDAVModule" />
  <remove name="Session" />
  <add name="SPRequestModule" preCondition="integratedMode" type="Microsoft.SharePoint.ApplicationRuntime.SPRequestModule, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  <add name="SharePoint14Module" preCondition="integratedMode" />
  <add name="StateServiceModule" type="Microsoft.Office.Server.Administration.StateModule, Microsoft.Office.Server, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="RSRedirectModule" type="Microsoft.ReportingServices.SharePoint.Soap.RSRedirectModule, RSSharePointSoapProxy, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
  <add name="PublishingHttpModule" type="Microsoft.SharePoint.Publishing.PublishingHttpModule, Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
</modules>

Result by supplemental approach:

<modules runAllManagedModulesForAllRequests="true">
  <remove name="FileAuthorization" />
  <remove name="Profile" />
  <remove name="WebDAVModule" />
  <remove name="Session" />
  <add name="SPRequestModule" preCondition="integratedMode" type="Microsoft.SharePoint.ApplicationRuntime.SPRequestModule, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  <add name="SharePoint14Module" preCondition="integratedMode" />
  <add name="StateServiceModule" type="Microsoft.Office.Server.Administration.StateModule, Microsoft.Office.Server, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="RSRedirectModule" type="Microsoft.ReportingServices.SharePoint.Soap.RSRedirectModule, RSSharePointSoapProxy, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
  <add name="PublishingHttpModule" type="Microsoft.SharePoint.Publishing.PublishingHttpModule, Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="CustomErrorHandler" type="com.handlers.CustomErrorHandler, Assemblyname, Version=4.5.6.7, Culture=neutral, PublicKeyToken=3450345789" />
</modules>

Like this thread says (confiremd by MS Developer) there is no way to control the order of nodes when working with SPWebConfigModification:

http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/154a09de-d9fc-4622-a397-589a3c1a87e5/

Adding the web.config entry manually is no option.

Is there really no other way?

Was it helpful?

Solution 2

Finally I ended up with a different approach for my custom error pages.

Because it is not possible to control the order of elements in web.config programmatically (which sucks so much!) and editing manually is no option, I gave up the approach with a HttpModule and manipulating web.config.

Instead I wrote a web application scoped feature receiver like described here: http://todd-carter.com/post/2010/04/07/An-Expected-Error-Has-Occurred.aspx to set a custom error page for the application (which handles exceptions). The custom error page has a control which renders a custom message for the user and logs the exception.

Additionally I added a custom 404.html file to the solution in the mapped LAYOUTS\1033 folder and registered that file for the application in the same feature receiver (see code below). The 404.html does a redirect to a .aspx-File. The aspx has a control which renders the content internationalized. You can also just set the static html for 404 errors. The reason for the redirect was internationalization.

One additional pitfall with the .html file and non-IE-browsers was for me the problem described here (because I copied the standard SP 404.html for a starting point):

http://andreasglaser.net/post/2009/03/15/SharePoint-and-custom-404-Page-Not-Found-and-UTF-8-issue-with-Firefox.aspx

Code goes here:

private const string CustomErrorPage = "/_layouts/CustomError.aspx";

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
  SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

  if (webApp != null)
  {
    if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, CustomErrorPage))
    {
      throw new ApplicationException("Cannot create the new error page mapping.");
    }
  }

  webApp.FileNotFoundPage = "Custom404.html";
  webApp.Update(true);
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
  SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

  if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, null))
  {
    throw new ApplicationException("Cannot reset the default error page mapping.");
  }
  webApp.FileNotFoundPage = String.Empty;
  webApp.Update(true);
}

OTHER TIPS

If it doesn't work as expected then I would recommend you to use Xpath together with SPWebModifications to define the order.

Update

I just tested it out my solution together with XPath solution and it's not behaving as expected. It seems that there is no way of controlling the order of entries. Thanks Per for his input!

Update 2

Could you please try using HttpApplication instead of HttpContext because HttpApplication defines the methods, properties, and events common to all application objects within an ASP.NET application. See more

Usage

 HttpContext context = ((HttpApplication)sender).Context;
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top