代码之家  ›  专栏  ›  技术社区  ›  eulerfx

用于执行.aspx文件的ASP.net IHttpHandler

  •  1
  • eulerfx  · 技术社区  · 16 年前

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

    1 回复  |  直到 16 年前
        1
  •  3
  •   chadmyers    16 年前

    这里有一种快速肮脏的方法:

    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);
    
    推荐文章