【问题标题】:Evaluating ASPX Pages from custom httpHandlers从自定义 httpHandler 评估 ASPX 页面
【发布时间】:2012-01-23 06:36:19
【问题描述】:

我到处寻找帮助,这让我很恼火。

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

我的愿景是有一个网址(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 将其呈现到响应中。

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

谢谢,

奥利弗

【问题讨论】:

  • 你考虑过使用 ASP.NET MVC 吗?似乎它非常适合您正在尝试做的事情。
  • 需要用于 .net 3.5,有好的例子吗?
  • MVC2 适用于 .NET 3.5。 nerddinner.codeplex.com 示例是典型示例。本质上,您需要一个标准的 TooController,其 Index 操作采用特定的工具 id。您可以添加其他操作或参数(或两者)来扩展功能。
  • 酷,我会研究一下,但目前我仍然对不需要额外库的解决方案持开放态度。
  • 另一个选项,看到你不能使用 .net 4.0(和 webforms 路由)是 IIS url rewrite 2.0。 iis.net/download/urlrewrite

标签: c# asp.net .net httphandler


【解决方案1】:

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

公共无效ProcessRequest(HttpContext上下文) { //注意:在这里你应该实现你的自定义映射 字符串 yourAspxFile = "~/Default.aspx"; //通过路径获取编译类型 类型类型 = BuildManager.GetCompiledType(yourAspxFile); //创建页面实例 Page page = (Page) Activator.CreateInstance(type); //处理请求 page.ProcessRequest(上下文); }

【讨论】:

    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多