=====HttpHandler的问题=====

happyjun2000 2004-09-11 10:08:22
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"] ;却什么都没有显示。
是什么原因 ?
...全文
254 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
happyjun2000 2005-06-21
  • 打赏
  • 举报
回复
遇到的问题是因为

配置
<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" 等(缩小范围,或者改动后缀名)
速马 2004-09-13
  • 打赏
  • 举报
回复
同思归,这种情况还是使用HttpModule合适
happyjun2000 2004-09-13
  • 打赏
  • 举报
回复
我要实现的简单功能如下 :
想在访问所有的*.aspx的页面时,用HttpHandler来处理,使得context中添加Items["address"] ,然后在页面显示的时候在pageload()中可以从context里面取出Items["address"]的值显示。
能不能提供一个可用HttpHandler来实现上面功能的代码参考。
happyjun2000 2004-09-13
  • 打赏
  • 举报
回复
用HttpHandler问题解决了。谢谢大家!
速马 2004-09-13
  • 打赏
  • 举报
回复
在HttpHandler里面做context.Server.Transfer肯定是没得问题的
但是你试验一下直接执行“/PaladinWebApplication/MyTest/Test1.aspx”会不会有问题?

还有,你是不是把*.aspx映射为你的Handler来处理了?如果是这样的话,你需要在ProcessRequest里面放上执行这个aspx页面的代码,而不能使用Server.Transfer了(因为Transfer或者Execute执行时还是会跑到HttpHandler上面来)

关于IsReusable属性,最好是设置为false(原理不是特别清楚,但是设置为true就容易出问题)
goody9807 2004-09-13
  • 打赏
  • 举报
回复
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 Handler

By 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.

leisang 2004-09-13
  • 打赏
  • 举报
回复
学习一下,做个标记
happyjun2000 2004-09-13
  • 打赏
  • 举报
回复
谢谢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 执行子请求时出错。
这是怎么回事 ?
happyjun2000 2004-09-13
  • 打赏
  • 举报
回复
谢谢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 执行子请求时出错。
这是怎么回事 ?
happyjun2000 2004-09-13
  • 打赏
  • 举报
回复
我看了下面的文章,做的上面的test
http://www.microsoft.com/china/MSDN/library/architecture/patterns/esp/ImpFrontControllerInASP.mspx
我不成功是为什么?
happyjun2000 2004-09-13
  • 打赏
  • 举报
回复
感谢上面大家对我的帮助!
Sunmast(速马, Reloading...) 同思归大哥说的对,用HttpModule合适比较合适。
但我这人有点死脑筋,只是想知道为什么用HttpHandler不能写,我明天结贴。
saucer 2004-09-11
  • 打赏
  • 举报
回复
Context.Items的东西只对当前请求有效,所以条件是,不过用什么,所执行编码必须属
于同一请求,否则请用Session or Application
saucer 2004-09-11
  • 打赏
  • 举报
回复
what is the relationship between Handler and your page?
huangsuipeng 2004-09-11
  • 打赏
  • 举报
回复
关键在于,该HANDLER调用了command.execute的虚方法,实际调用了实例的函数,其中使用了
server.transfer到实际页面,使用了同一个CONTEXT
happyjun2000 2004-09-11
  • 打赏
  • 举报
回复
to saucer(思归/MVP)

我是参考的
使用 Microsoft .NET 的企业解决方案模式 > Web 表示模式 > 在 ASP.NET 中使用 HTTPHandler 实现 Front Controller
一书中的示例代码来实现的,上面的代码就是那里的,为什么在HttpHandler中就不能实现我要的效果了,为什么 ? 是不是那个示例是错的 。
saucer 2004-09-11
  • 打赏
  • 举报
回复
那么你不该用HttpHandler,用HttpModule, 参考

http://www.c-sharpcorner.com/Code/2004/Jan/AudioVideo4ASPNetStarterKit.asp
savagewang1978 2004-09-11
  • 打赏
  • 举报
回复
<httpHandlers>
<add verb="*" path="*.aspx0" type="Paladin.FrontController.Handler,PaladinWebApplication" />
</httpHandlers>

我只是想在页面加载的时候输出我家加在context中的东西。
happyjun2000 2004-09-11
  • 打赏
  • 举报
回复
<httpHandlers>
<add verb="*" path="*.aspx0" type="Paladin.FrontController.Handler,PaladinWebApplication" />
</httpHandlers>

我只是想在页面加载的时候输出我家加在context中的东西。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧