使IHttpHandler有一个现有的.aspx页面来处理请求是什么?我希望能够将.aspx文件编译成IHttpHandler,然后让它处理请求。有PageParser.GetCompiledPageInstance方法,但是在文档中它声明它不是直接用于代码。我知道我可以将apsx文件自动定向到或执行RewritePath,但是我想让对象引用处理程序。
答案 0 :(得分:3)
这是一种快速且肮脏的方式:
var virtualPath = "~/foo/bar.aspx"
var output = HttpContext.Current.Response.Output;
// Get the compiled page type (i.e. foo_bar_aspx)
Type controlType = BuildManager.GetCompiledType(virtualPath);
// "new()" it up
var pageInstance = Activator.CreateInstance(controlType);
// Execute it
HttpContext.Current.Server.Execute(pageInstance, output, true);