2011-12-19 98 views
6

我在世界各地都有搜索寻求帮助,并开始烦我。评估自定义httpHandlers的ASPX页面

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

我的愿景是拥有一个网址(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.aspx),我希望我的客户的HttpHandler来渲染到响应。

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

感谢,

奥利弗

+3

你有没有使用考虑ASP.NET MVC?看起来它很适合你想要做的事情。 – tvanfosson 2011-12-19 00:59:48

+0

需要为.NET 3.5,有一个很好的例子吗? – 2011-12-19 01:03:00

+0

MVC2适用于.NET 3.5。 http://nerddinner.codeplex.com/示例是典型示例。从本质上讲,你需要一个标准的TooController,它带有一个采用特定工具ID的索引操作。您可以添加其他操作或参数(或两者)来扩展功能。 – tvanfosson 2011-12-19 01:06:50

回答

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