如何从 ASP.NET 4.0 Web 应用程序中禁用/删除 WebPageHttpModule?
我正在尝试获取我的 通配符 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试关闭 ASP.NET 网页,您可以在应用程序设置中设置此标志:
If you're trying to turn off ASP.NET webpages, you can set this flag in app settings:
刚刚进行了一番挖掘,其中的一些代码至少可以说很粗糙。
以下是 WebPageHttpHandler 中的一段代码:
如果您愿意使用反射并且在完全信任中运行,则可以通过反射访问 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:
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.