然而手机客户端又不支持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, "&cid=" + HttpContext.Current.Request["cid"]);
}
return result;
}
}
}
}
以下是配置方法和源码。
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, "&cid=" + HttpContext.Current.Request["cid"]);
}
return result;
}
}
}
}
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“asp.net通过HttpModule自动在Url地址上添加参数”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。