c# - 从自定义 httpHandlers 评估 ASPX 页面

标签 c# asp.net .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();
        }
    }
}

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

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

谢谢,

奥利弗

最佳答案

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

关于c# - 从自定义 httpHandlers 评估 ASPX 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8556112/

相关文章:

javascript - 为什么 ASP.net RadioButtonList onchange 客户端事件不触发?

.net - 如何使用.net中的服务帐户访问域用户的日历?

java - .Net 和 Java 接口(interface)是否足够相似,可以以某种方式进行整合?

c# - InternalsVisibleTo 不起作用

c# - 使用列表框进行数据绑定(bind)

c# - 在与角色 Controller /C# 发生碰撞时更改立方体的颜色

c# - 在 VS for mac 上运行项目

asp.net - asp.net 中的 Roslyn 编译器 - 未拾取条件编译符号

javascript - 从脚本中获取 System.Type 实例 (ClearScript)

c# - cmd.ExecuteNonQuery 第 i 个整数的值从 0 变为 -1