在处理静态资源请求时获取Session的疑问。

chillystar 2015-06-26 10:52:33
在做一个伪静态处理,重写了HttpModule。

处理是在AcquireRequestState事件,已经通过web.config,设置所有的请求都经过AcquireRequestState。也就是说无论静态、动态或是任何态,只要是请求一定会引发AcquireRequestState事件。

但在调试时出现以下问题:
* 请求是一个准确的aspx或ashx页面时,正常获取session。
* 请求是一个存在的静态页面时,session=null。
* 请求是一个伪静态地址时(不存在该文件或虚拟目录),session=null。

测试过程:
第一步:先提交http://localhost:44172/default.aspx,结果为:
WebModule.context_AcquireRequestState:Session Enabled!
第二步:重新提交http://localhost:44172/default.aspx,结果为:
this's a test
WebModule.context_AcquireRequestState:Session Enabled!
第二步:提交http://localhost:44172/test.htm,结果为:
【显示test.htm登录页面】<--页面内容
WebModule.context_AcquireRequestState:Session Disable!
第四步:提交http://localhost:44172/gn=sim/login(红色部分是是一个作为参数的伪静态地址,实际并不存在。)结果:
WebModule.context_AcquireRequestState:Session Disable!


请问该如何处理,才能让静态资源也能取得Session?


关键代码如下:

public class WebModule : System.Web.IHttpModule, System.Web.SessionState.IRequiresSessionState
{
public void Init(System.Web.HttpApplication context)
{
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
}
void context_AcquireRequestState(object sender, EventArgs e)
{
System.Web.HttpContext context = (sender as System.Web.HttpApplication).Context;

// 处理伪静态地址信息。
if (context.Session == null) // Session空,未登录,调用并显示登录页面。
{
using (System.IO.FileStream fs = new System.IO.FileStream("d:\\webdata\\HTMLLogin.htm", System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
byte[] cxt = new byte[fs.Length];
fs.Read(cxt, 0, cxt.Length);
fs.Close();
context.Response.BinaryWrite(cxt);
}
}
else
{
if(context.Session["test"]==null)
{
context.Session.Add("test","this's a test");
}
else
{
context.Response.Write("<h4>WebModule.context_AcquireRequestState Value=:"+context.Session["test"].ToString()+"</h4><br />");
}
}

// 打印session测试结果。
context.Response.Write("<h4>WebModule.context_AcquireRequestState</h4><br />");
try
{
if (context.Session != null)
{
context.Response.Write("<h4 style='color:#ff0000;'>WebModule.context_AcquireRequestState:Session Enabled!</h4><br />");
}
else
{
context.Response.Write("<h4 style='color:#0000ff;'>WebModule.context_AcquireRequestState:Session Disable!</h4><br />");
}
}
catch (Exception)
{
context.Response.Write("<h4 style='color:#0000ff;'>WebModule.context_AcquireRequestState:Session Error!</h4><br />");
}
}
context.ApplicationInstance.CompleteRequest();
}

Web.config的相关配置修改:

<system.web>
……
<httpModules>
<add name="WebModule" type="WebApp.WebModule,WebApp" />
</httpModules>
……
</system.web>
<system.webServer>
……
<modules runAllManagedModulesForAllRequests="true">
……
</system.webServer>
...全文
470 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
於黾 2015-06-26
  • 打赏
  • 举报
回复
如果你用IE直接访问一个静态文件,没有任何转向到aspx页面的操作 那么就根本和asp.net程序无关啊,IIS自己就把静态资源发送给IE了,你的程序根本什么都不用管
chillystar 2015-06-26
  • 打赏
  • 举报
回复
引用 3 楼 sp1234 的回复:
所谓静态的意思 --> 所谓伪静态的意思 其实伪静态还是不是”伪静态“,根本点不是在于其url的后缀是什么,关键在于内部机制。假设人家请求的路径是 http://localhost:44172/default.ccav http://localhost:44172/default.mp4 http://localhost:44172/default.1234567.doc http://localhost:44172/default?id=1234567 只要是内部是转而调用aspx的aspx、ashx等,而不懂的人偏要说什么“这是静态文件”,那么就是伪静态的。 所以这里的机制很重要。而你没有走aspx或者ashx这个机制,你在既处理了一下返回结果,就说它是“伪静态的”,有些标题党。
明白,谢谢了。对于原本已经违背原则的事,看来还是及早终结并另寻它法为好。
chillystar 2015-06-26
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
所谓静态的意思,是指当url中访问一个.html/.htm后缀的路径时,系统asp.net应用程序使用
Server.Transfer(....)
(并且同时传送所有Form Datas)去访问一个aspx。然后在这个aspx中访问Session集合。 而你根本没有访问aspx啊。
你说得一点都不错,确实没有访问aspx。在某些场合下,根本没打算访问任何aspx或ashx之类的页面,仅仅就是为了在AcquireRequestState中处理完毕。 或者换个更直白的形式请教: 期望是以单纯htm或伪静态地址,在AcquireRequestState获取到Session。这种想法是否会违背.net的原则而无法实现?若能实现,请问具体方式怎样?
  • 打赏
  • 举报
回复
所谓静态的意思 --> 所谓伪静态的意思 其实伪静态还是不是”伪静态“,根本点不是在于其url的后缀是什么,关键在于内部机制。假设人家请求的路径是 http://localhost:44172/default.ccav http://localhost:44172/default.mp4 http://localhost:44172/default.1234567.doc http://localhost:44172/default?id=1234567 只要是内部是转而调用aspx的aspx、ashx等,而不懂的人偏要说什么“这是静态文件”,那么就是伪静态的。 所以这里的机制很重要。而你没有走aspx或者ashx这个机制,你在既处理了一下返回结果,就说它是“伪静态的”,有些标题党。
  • 打赏
  • 举报
回复
在你的所有描述中,”伪静态处理“这个词儿的机制都是另外一种意思,不是asp.net的伪静态的概念哦。
  • 打赏
  • 举报
回复
所谓静态的意思,是指当url中访问一个.html/.htm后缀的路径时,系统asp.net应用程序使用
Server.Transfer(....)
(并且同时传送所有Form Datas)去访问一个aspx。然后在这个aspx中访问Session集合。 而你根本没有访问aspx啊。

62,243

社区成员

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

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

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

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