How can I disable/remove the WebPageHttpModule from ASP.NET 4.0 web applications?
Asked Answered
F

2

10

I'm trying to get my wildcard http handler to handle *.cshtml pages but the request never reaches my handler as it looks like it's being intercepted by WebPageHttpModule that I discovered exists via this StackTrace:

[HttpException (0x80004005): Exception of type 'System.Web.HttpException' was thrown.]
   System.Web.WebPages.ApplicationStartPage.ExecuteStartPage(HttpApplication application, Action`1 monitorFile, Func`2 fileExists, Func`2 createInstance, IEnumerable`1 supportedExtensions) +88
   System.Web.WebPages.ApplicationStartPage.ExecuteStartPage(HttpApplication application) +287
   System.Web.WebPages.WebPageHttpModule.StartApplication(HttpApplication application, Action`1 executeStartPage, EventHandler applicationStart) +113
   System.Web.WebPages.WebPageHttpModule.StartApplication(HttpApplication application) +71
   System.Web.WebPages.WebPageHttpModule.Init(HttpApplication application) +156
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +431
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +194
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +339
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +253

[HttpException (0x80004005): Exception of type 'System.Web.HttpException' was thrown.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8972180
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +256

I've tried to disable all HttpModules using the Web.Config but this has no effect, (it also doesn't appear in IIS HttpModules section):

<system.web>
    <httpModules>
        <clear/>
    </httpModules>
</system.web>
    ....
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <clear/>
    </modules>
</system.webServer>

Hunting around with ILSpy reveals the following code in System.Web.WebPages.dll is what's registering the WebPageHttpModule:

namespace System.Web.WebPages
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    public static class PreApplicationStartCode
    {
        private static bool _startWasCalled;
        public static void Start()
        {
            if (PreApplicationStartCode._startWasCalled)
            {
                return;
            }
            PreApplicationStartCode._startWasCalled = true;
            WebPageHttpHandler.RegisterExtension("cshtml");
            WebPageHttpHandler.RegisterExtension("vbhtml");
            PageParser.EnableLongStringsAsResources = false;
            DynamicModuleUtility.RegisterModule(typeof(WebPageHttpModule));
            ScopeStorage.CurrentProvider = new AspNetRequestScopeStorageProvider();
        }
    }
}

But it's a vanilla .NET 4.0 application and I don't have a references to any System.Web.WebPages, MVC or Razor dlls I'm only referencing System.Web.

So how are these assemblies getting loaded, why is WebPageHttpModule being registered and how can I remove/disable it?

Formicary answered 26/6, 2011 at 11:40 Comment(0)
M
8

If you're trying to turn off ASP.NET webpages, you can set this flag in app settings:

<add key="webpages:Enabled" value="false" />
Merrymaker answered 27/6, 2011 at 1:10 Comment(2)
This is not working for me. I still end up with 404's for all URLs ending in .cshtml I'm trying to write a source-code browser, and I cannot have it failing on any URL extension. Any suggestions?Higley
Oh, I forgot to mention that I have a separate question for this: #7211015Higley
B
2

Just had a dig around and some of the code in there is .. rough, to say the least.

Here's a snip of code from the WebPageHttpHandler:

namespace System.Web.WebPages
{
  public class WebPageHttpHandler : IHttpHandler, IRequiresSessionState
  {
    private static List<string> _supportedExtensions = new List<string>();

    public static void RegisterExtension(string extension)
    {
      WebPageHttpHandler._supportedExtensions.Add(extension);
    }

  // [snip]
}

If you're willing to use reflection and you're running in Full Trust, you could access the _supportedExtensions static private field of the WebPageHttpHandler via Reflection, and remove the cshtml and vbhtml items from the list in your own PreApplicationInit handler. From what I could see in the DynamicModuleUtility, removing the WebPageHttpHandler registration would be more involved than that.

Ban answered 26/6, 2011 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.