从自定义 httpHandler 评估 ASPX 页面

发布于 2024-12-21 17:59:08 字数 1181 浏览 1 评论 0原文

我到处寻求帮助,但它开始让我烦恼。

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

我的愿景是有一个网址(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();
        }
    }
}

基本上,我在“工具”文件夹 (Tool\tool.aspx) 中有一个页面,我希望我的客户 httpHandler 将其渲染到响应中。

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

谢谢,

奥利弗

I have search everywhere for help and its starting to annoy me.

I am creating an Internal Tooling Website which stores Tools and their related information.

My vision is to have a web address (Http://website.local/Tool/ID)
Where ID is the ID of the Tool we want displayed.
My reasoning is then I can extend the functionality of the URL to allow for various other functions.

Currently I use a custom httpHandler which intercepts any URL which is in the 'Tool' Folder.

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

Basically I have a page inside the 'Tool' folder (Tool\tool.aspx) which I want my customer httpHandler to Render into the Response.

But this method doesn't work (It doesn't fail, just doesn't show anything) I can write the raw file to the response but obviously thats not my goal.

Thanks,

Oliver

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2024-12-28 17:59:08

如果您仍然想使用自定义方法,可以尝试在 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);
        }

If you still want to use your custom approach, you can try to do the following in your IHttpHandler derived class:

        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);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文