从自定义httpHandlers评估ASPX页面

Oli*_*ker 6 .net c# asp.net httphandler

我到处寻找帮助,开始惹恼我.

我正在创建一个内部工具网站,用于存储工具及其相关信息.

我的愿景是拥有一个网址(Http://website.local/Tool/ID)其中ID是我们想要显示的工具的ID.我的理由是,我可以扩展URL的功能以允许各种其他功能.

目前我使用自定义的httpHandler拦截"工具"文件夹中的任何URL.

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Tooling_Website.Tool
{
    public class ToolHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }


        public void ProcessRequest(HttpContext context)
        {
            //The URL that would hit this handler is: http://{website}/Tool/{AN ID eg: http://{website}/Tool/PDINJ000500}
            //The idea is that what would be the page name is now the ID of the tool.
            //tool is an ASPX Page.
            tool tl = new tool();
            System.Web.UI.HtmlTextWriter htr = new System.Web.UI.HtmlTextWriter(context.Response.Output);
            tl.RenderControl(htr);
            htr.Close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上我在'Tool'文件夹(Tool\tool.aspx)中有一个页面,我希望我的客户httpHandler渲染到Response中.

但是这种方法不起作用(它没有失败,只是没有显示任何内容)我可以将原始文件写入响应,但显然这不是我的目标.

谢谢,

奥利弗

Ily*_*luk 5

如果您仍想使用自定义方法,可以尝试在IHttpHandler派生类中执行以下操作:

        public void ProcessRequest(HttpContext context)
        {
            //NOTE: here you should implement your custom mapping
            string yourAspxFile = "~/Default.aspx";
            //Get compiled type by path
            Type type = BuildManager.GetCompiledType(yourAspxFile);
            //create instance of the page
            Page page = (Page) Activator.CreateInstance(type);
            //process request
            page.ProcessRequest(context);
        }