为此,您需要注册一个处理.aspx
文件的页面工厂。所以首先创建一个扩展的新类PageHandlerFactory
:
public class MyPageFactory : PageHandlerFactory
{
public override IHttpHandler GetHandler(
HttpContext httpContext, string requestType, string url,
string pathTranslated)
{
// Here you can inspect `HttpContext` and perform whatever checks you
// need to determine whether or not to use your custom overridden page.
if (shouldOverride)
{
var newVirtualPath = "/Overrides/Foo/MyPage.aspx";
string newFilePath = httpContext.Server.MapPath(newVirtualPath);
// Now create the page instance
IHttpHandler page = PageParser.GetCompiledPageInstance(newVirtualPath, newFilePath, httpContext);
return page;
}
else
{
// If we're not overriding, just return the default implementation
return base.GetHandler(httpContext, requestType, url, pathTranslated);
}
}
}
不要忘记在您的web.config
(IIs7) 中注册它:
<system.webServer>
<httpHandlers>
<add verb="*" path="*.aspx" type="MyPageFactory" />
</httpHandlers>
</system.webServer>
或 < IIS7:
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx" type="MyPageFactory" />
</httpHandlers>
</sysetm.web>