我试图拦截Asp.Net中页面的调用,所以我可以在使用之前更改对象。
我提供了一个简化的代码来说明问题
通过调试,我进入CustomPageHandlerFactory,我看到它使用基本方法来获取页面的实例。但这个例子似乎没有被推进
gethandler对象的哈希码和页面实例的哈希码不同。
它们不应该相同吗?
页面:
public partial class _Default : Page
{
public bool Throw { get; set; }
public _Default()
{
Throw = true;
}
protected void Page_Load(object sender, EventArgs e)
{
ThrowException();
}
public virtual void ThrowException()
{
if (Throw)
{
throw new Exception("Non sense error!");
}
}
}
处理程序:
public class CustomPageHandlerFactory : PageHandlerFactory
{
public override IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
{
var page = base.GetHandler(context, requestType, virtualPath, path);
((_Default)page).Throw = false;
return page;
}
}
和配置的一大块:
(...)
<system.webServer>
<handlers>
<add name="CustomPageHandler" path="*.aspx" verb="*" type="Example.CustomPageHandlerFactory" />
</handlers>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<system.web>
<httpHandlers>
<remove verb="*" path="*.aspx" />
<add verb="*" path="*.aspx" type="Example.CustomPageHandlerFactory" />
</httpHandlers>
(...)
正如@Ondrej Svejdar所指出的,我应该添加框架版本: