2008-12-12 61 views
1

正确的做一个IHttpHandler有一个现有的.aspx页面处理请求?我希望能够将.aspx文件编译为IHttpHandler,然后让它处理请求。有PageParser.GetCompiledPageInstance方法,但是在文档中声明它不适用于从代码直接使用。我知道我可以将apsx文件自动引导到或执行RewritePath,但是我希望将对象引用到处理程序。ASP.net IHttpHandler执行.aspx文件

回答

3

下面是做这件事的一个快速N'-二肮脏的方式:

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