如何从 ASP.NET 4.0 Web 应用程序中禁用/删除 WebPageHttpModule?

发布于 2024-11-17 03:06:19 字数 3229 浏览 2 评论 0原文

我正在尝试获取我的 通配符 http 处理程序处理 *.cshtml 页面 但请求永远不会到达我的处理程序,因为它看起来像是被我通过此 StackTrace 发现存在的 WebPageHttpModule 拦截:

[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

我尝试禁用所有使用 Web.Config 的 HttpModules 但这没有任何效果(它也不会出现在 IIS HttpModules 部分中):

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

使用 ILSpy 查找会发现 System.Web.WebPages.dll 中的以下代码是注册 WebPageHttpModule< /strong>:

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

但它是一个普通的 .NET 4.0 应用程序,我没有对任何 System.Web.WebPages、MVC 或 Razor dll 的引用我引用System.Web

那么这些程序集是如何加载的,为什么要注册 WebPageHttpModule 以及如何删除/禁用它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤还要旧人安 2024-11-24 03:06:19

如果您尝试关闭 ASP.NET 网页,您可以在应用程序设置中设置此标志:

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

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

<add key="webpages:Enabled" value="false" />
凝望流年 2024-11-24 03:06:19

刚刚进行了一番挖掘,其中的一些代码至少可以说很粗糙。

以下是 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]
}

如果您愿意使用反射并且在完全信任中运行,则可以通过反射访问 WebPageHttpHandler 的 _supportedExtensions 静态私有字段,并从列表中删除 cshtml 和 vbhtml 项在您自己的 PreApplicationInit 处理程序中。从我在 DynamicModuleUtility 中看到的情况来看,删除 WebPageHttpHandler 注册会比这更复杂。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文