开发者

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

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()
        {
            i开发者_如何学Cf (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?


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

<add key="webpages:Enabled" value="false" />


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜