当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net通过HttpModule自动在Url地址上添加参数

asp.net通过HttpModule自动在Url地址上添加参数

2017年12月12日  | 移动技术网IT编程  | 我要评论

火鸡枪,西安门户,雨石秋影

然而手机客户端又不支持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;
}
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网