然而手机客户端又不支持Session和Cookie传值,其他方法给页面赋值再传值显得太麻烦了,而且还不易维护,容易弄丢出错,于是想到了用HttpModule来把cid参数赋在Url地址上,让url把cid参数每页自动传递下去,需要用cid时只要通过Requet["cid"]获取,这样就不用为传值而烦恼了。
以下是配置方法和源码。

1)在web.config配置文件中添加以下节点
复制代码 代码如下:
<httpModules>
<add name="HttpModule" type="ThreeHegemony.Utility.AutoAddCid"/>
</httpModules>


2)通过继承IHttpModule来实现url传值。

代码
复制代码 代码如下:
using System;
using System.Text;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
namespace ThreeHegemony.Utility
{
/// <summary>
/// Auther: Jess.zou
/// Create data: 2009-08-06
/// Description: 该类作用在Url地址后自动添加(cid)
/// </summary>
public class AutoAddCid : System.Web.IHttpModule
{
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute += new EventHandler(this.OnPreSendRequestContent);
}
protected void OnPreSendRequestContent(Object sender, EventArgs e)
{
System.Web.HttpApplication myContext = (System.Web.HttpApplication)sender;
myContext.Response.Filter = new AppendSIDFilter(myContext.Response.Filter);
}
private void ReUrl_BeginRequest(object sender, EventArgs e)
{
string cid = "";
string url = "";
HttpContext context = ((HttpApplication)sender).Context;
if (string.IsNullOrEmpty(context.Request.QueryString["cid"]))
{
if (context.Request.QueryString.Count == 0)
{
url = string.Format("{0}?cid={1}", context.Request.RawUrl, cid);
}
else
{
url = string.Format("{0}&cid={1}", context.Request.RawUrl, cid);
}
}
context.RewritePath(url);
}
public void Dispose() { }
public class AppendSIDFilter : Stream
{
private Stream Sink { get; set; }
private long _position;
private System.Text.StringBuilder oOutput = new StringBuilder();
public AppendSIDFilter(Stream sink)
{
Sink = sink;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override long Length
{
get { return 0; }
}
public override long Position
{
get { return _position; }
set { _position = value; }
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
return Sink.Seek(offset, direction);
}
public override void SetLength(long length)
{
Sink.SetLength(length);
}
public override void Close()
{
Sink.Close();
}
public override void Flush()
{
Sink.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return Sink.Read(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
{
if (string.IsNullOrEmpty(HttpContext.Current.Request["cid"]))
{
Sink.Write(buffer, 0, buffer.Length);
return;
}
string content = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
Regex regex = new Regex(RegexResource.HREF, RegexOptions.IgnoreCase);
Regex action_regex = new Regex(RegexResource.ACTION, RegexOptions.IgnoreCase);
if (regex.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.HREF, new MatchEvaluator(ReplaceSID), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
if (action_regex.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.ACTION, new MatchEvaluator(ReplaceSID), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(content);
Sink.Write(data, 0, data.Length);
}
public static string ReplaceSID(Match match)
{
if (match.Value.IndexOf("cid=") != -1)
{
return match.Value;
}
string result;
if (match.Value.IndexOf('?') == -1)
{
result = match.Value.Insert(match.Value.Length - 1, "?cid=" + HttpContext.Current.Request["cid"]);
}
else
{
result = match.Value.Insert(match.Value.Length - 1, "&amp;cid=" + HttpContext.Current.Request["cid"]);
}
return result;
}
}
}
}
标签:
asp.net,HttpModule,Url添加参数,

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?