一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

asp.net通過HttpModule自動(dòng)在Url地址上添加參數(shù)

然而手機(jī)客戶端又不支持Session和Cookie傳值,其他方法給頁面賦值再傳值顯得太麻煩了,而且還不易維護(hù),容易弄丟出錯(cuò),于是想到了用HttpModule來把cid參數(shù)賦在Url地址上,讓url把cid參數(shù)每頁自動(dòng)傳遞下去,需要用cid時(shí)只要通過Requet["cid"]獲取,這樣就不用為傳值而煩惱了。
以下是配置方法和源碼。

1)在web.config配置文件中添加以下節(jié)點(diǎn)
復(fù)制代碼 代碼如下:
<httpModules>
<add name="HttpModule" type="ThreeHegemony.Utility.AutoAddCid"/>
</httpModules>


2)通過繼承IHttpModule來實(shí)現(xiàn)url傳值。

代碼
復(fù)制代碼 代碼如下:
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地址后自動(dòng)添加(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;
}
}
}
}

AspNet技術(shù)asp.net通過HttpModule自動(dòng)在Url地址上添加參數(shù),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 久久久久亚洲国产 | 久草cm| 99热这里只有精品国产动漫 | 国产美女在线精品免费观看 | 黄色影片免费观看 | 91热视频 | 久久中文字幕综合不卡一二区 | 亚洲国产精品成人综合色在线婷婷 | 亚洲精品天堂自在久久77 | 国产精品久久久久久永久牛牛 | 中国日韩欧美中文日韩欧美色 | 国产视频中文字幕 | 久久婷婷激情 | 国产成人精品永久免费视频 | 在线观看免费污视频 | 久久97久久97精品免视看秋霞 | 日本99热 | 亚洲精品在线视频 | 一区二区三区在线看 | 欧美日韩一区二区三区自拍 | 久久久久久亚洲精品 | 亚洲免费视频一区二区三区 | 色哟在线 | 日本久久网站 | 被公侵犯肉体中文字幕一区二区 | 久久精品福利 | 天天舔天天爱 | 久久综合一区 | 波多野结衣在线影院 | 四虎影视在线观看永久地址 | 四虎 2022 永久网站 | 91网页版| 色哟哟导航 | 国产成人精品福利网站在线观看 | 一二三区无线码2021 | 亚洲国产色婷婷精品综合在线观看 | 亚洲欧美在线综合一区二区三区 | 国产视频播放 | 午夜激情视频 | 高清视频黄色录像免费 | 亚洲一区在线观看视频 |