4

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?

1
  • Could you please share the code of HttpModule, and how are you handling the request? – Jun 5, 2012 at 12:40

2 Answers 2

2

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;
11
  • The requirement in the question was to put the module at the begining of to <Modules> section which you can't do with SPWebConfigModifications Jun 5, 2012 at 11:46
  • I've just tested you code and it puts MyModuleName as the LAST entry in modules Jun 5, 2012 at 11:59
  • They seems to be added alphabetically but after all the standard entries. And as the link in the question confirms there is no way of controlling it Jun 5, 2012 at 12:14
  • Yeah, I just tested it out myself with XPath and it's not behaving as expected. It seems there is no way of controlling the order. Jun 5, 2012 at 12:18
  • That's really poor. So the only solution is to edit the web.config manually...
    – Tom Bola
    Jun 5, 2012 at 12:22
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.