using System;
using System.Web;public class Handler : IHttpHandler
{
public void ProcessRequest( HttpContext context ) 
{
// context.Response.Write( "Hello World!" );
context.Items.Add( "address" , "Paladin-Address" ) ;
// context.Items["address"] = "Paladin-Address" ;
// context.Items["site"] = "Paladin-Site" ;
} public bool IsReusable
{  get { return true;} 
} }
 protected void Page_Load(object sender, System.EventArgs e)
   {
      if(!IsPostBack)
      {
         Label1.Text = (string)Context.Items["address"] ;
      }
   }上面的Handler 已经在web.config中配置过了。
我把注释中的context.Response.Write( "Hello World!" );写入程序,页面可以输出Hello World
但是我执行context.Items.Add( "address" , "Paladin-Address" ) ;
然后在页面中执行Label1.Text = (string)Context.Items["address"] ;却什么都没有显示。
是什么原因 ?

解决方案 »

  1.   

    what is the relationship between Handler and your page?
      

  2.   

    <httpHandlers>
     <add verb="*" path="*.aspx0" type="Paladin.FrontController.Handler,PaladinWebApplication" />
    </httpHandlers>我只是想在页面加载的时候输出我家加在context中的东西。
      

  3.   

    <httpHandlers>
     <add verb="*" path="*.aspx0" type="Paladin.FrontController.Handler,PaladinWebApplication" />
    </httpHandlers>我只是想在页面加载的时候输出我家加在context中的东西。
      

  4.   

    那么你不该用HttpHandler,用HttpModule, 参考http://www.c-sharpcorner.com/Code/2004/Jan/AudioVideo4ASPNetStarterKit.asp
      

  5.   

    to saucer(思归/MVP)我是参考的
     使用 Microsoft .NET 的企业解决方案模式 > Web 表示模式 > 在 ASP.NET 中使用 HTTPHandler 实现 Front Controller 
     一书中的示例代码来实现的,上面的代码就是那里的,为什么在HttpHandler中就不能实现我要的效果了,为什么 ? 是不是那个示例是错的 。
      

  6.   

    关键在于,该HANDLER调用了command.execute的虚方法,实际调用了实例的函数,其中使用了
    server.transfer到实际页面,使用了同一个CONTEXT
      

  7.   

    Context.Items的东西只对当前请求有效,所以条件是,不过用什么,所执行编码必须属
    于同一请求,否则请用Session or Application
      

  8.   

    谢谢saucer(思归/MVP)大哥
    我用你的用HttpModule来实现是可以的。
    但是我如果在HttpHandler中来实现,我执行
    public void ProcessRequest( HttpContext context ) 
    {
    string url = String.Format("{0}?{1}",
    context.Request.Url.AbsolutePath,
    context.Request.Url.Query);
    context.Server.Transfer ( url ) ;
    }
    是不是能把当前的context传递到我要访问的页面,但是提示我错误 :
    为 /PaladinWebApplication/MyTest/Test1.aspx 执行子请求时出错。
    这是怎么回事 ?
      

  9.   

    谢谢huangsuipeng(hsp-ec.net)大哥!
    你说 :
    关键在于,该HANDLER调用了command.execute的虚方法,实际调用了实例的函数,其中使用了
    server.transfer到实际页面,使用了同一个CONTEXT使用了server.transfer到实际页面,使用了同一个CONTEXT ?
    那不是就是说把context传递下去了吗?为什么还不能在页面中处理呢 ?
    我后来向以下方法来实现手动的Server.Transfer () 来传递context,
    public void ProcessRequest( HttpContext context ) 
    {
    string url = String.Format("{0}?{1}",
    context.Request.Url.AbsolutePath,
    context.Request.Url.Query);
    context.Server.Transfer ( url ) ;
    }
    是不是能把当前的context传递到我要访问的页面,但是提示我错误 :
    为 /PaladinWebApplication/MyTest/Test1.aspx 执行子请求时出错。
    这是怎么回事 ?
      

  10.   

    Configuring HTTP Handlers 
    The <httpHandlers> configuration section handler is responsible for mapping incoming URLs to the IHttpHandler or IHttpHandlerFactory class. It can be declared at the computer, site, or application level. Subdirectories inherit these settings.Administrators use the <add> tag directive to configure the <httpHandlers> section. <Add> directives are interpreted and processed in a top-down sequential order. Use the following syntax for the <httpHandler> section handler:<httpHandlers>
    <add verb="[verb list]" path="[path/wildcard]" type="[COM+ Class], [Assembly]" validate="[true/false]" />
    <remove verb="[verb list]" path="[path/wildcard]" />
    <clear />
    </httpHandlers> Creating HTTP Handlers 
    To create an HTTP handler, you must implement the IHttpHandler interface. The IHttpHandler interface has one method and one property with the following signatures: 
    void ProcessRequest(HttpContext);
    bool IsReusable {get;}Customized Http HandlerBy customizing http handlers, new functionalities can be added to Web Server. Files with new extensions like .text for a text file can be handled by Web Server by using http handlers. The future of customization can lead to hosting .jsp pages in IIS by finding adequate ISAPI extensions. The following steps are involved to create customized http handler:
    1. Create a C# class library as 揈xamplehandler?br> 2. Name class as 揌andlerclass.cs?/font>using System;
    using System.Web;
    using System.Web.SessionState;
    namespace ExampleHandler
    {
    /// <summary>
    /// Summary description for Examplehandler.
    /// </summary>
    public class Handlerclass : IHttpHandler
    {
    public Handlerclass()
    {
    //
    // TODO: Add constructor logic here
    //
    }#region Implementation of IHttpHandler
    public void ProcessRequest(System.Web.HttpContext context)
    {
    HttpResponse objResponse = context.Response ;
    HttpSessionState objSession = context.Session ;
    objResponse.Write("<html><body><h1>Hello World from Handler") ;
    objResponse.Write("</body></html>") ;
    }public bool IsReusable
    {
    get
    {
    return true;
    }
    }
    #endregion
    }
    } Compile it and place it in the bin directory of TestApp project.
      

  11.   

    在HttpHandler里面做context.Server.Transfer肯定是没得问题的
    但是你试验一下直接执行“/PaladinWebApplication/MyTest/Test1.aspx”会不会有问题?还有,你是不是把*.aspx映射为你的Handler来处理了?如果是这样的话,你需要在ProcessRequest里面放上执行这个aspx页面的代码,而不能使用Server.Transfer了(因为Transfer或者Execute执行时还是会跑到HttpHandler上面来)关于IsReusable属性,最好是设置为false(原理不是特别清楚,但是设置为true就容易出问题)
      

  12.   

    我要实现的简单功能如下 :
    想在访问所有的*.aspx的页面时,用HttpHandler来处理,使得context中添加Items["address"] ,然后在页面显示的时候在pageload()中可以从context里面取出Items["address"]的值显示。
    能不能提供一个可用HttpHandler来实现上面功能的代码参考。
      

  13.   

    同思归,这种情况还是使用HttpModule合适
      

  14.   

    感谢上面大家对我的帮助!
    Sunmast(速马, Reloading...) 同思归大哥说的对,用HttpModule合适比较合适。
    但我这人有点死脑筋,只是想知道为什么用HttpHandler不能写,我明天结贴。
      

  15.   

    我看了下面的文章,做的上面的test
    http://www.microsoft.com/china/MSDN/library/architecture/patterns/esp/ImpFrontControllerInASP.mspx
    我不成功是为什么?
      

  16.   

    用HttpHandler问题解决了。谢谢大家!
      

  17.   

    遇到的问题是因为配置
    <httpHandlers>
    <add verb="*" path="*.aspx" type="Paladin.FrontController.Handler,PaladinWebApplication" />
    </httpHandlers>
    注意这里是处理所有的*.aspx,那么如果在语句
    context.Server.Transfer ( aa.aspx ) ;
    因为转向的是aspx后缀名的,要和配置中的path="*.aspx" 发生冲突,一般会提示错误 :
    为 /PaladinWebApplication/MyTest/Test1.aspx 执行子请求时出错。所以需要改动配置为path="*.aspx0" 等(缩小范围,或者改动后缀名)