asp.net通過HttpModule自動(dòng)在Url地址上添加參數(shù)
以下是配置方法和源碼。
1)在web.config配置文件中添加以下節(jié)點(diǎn)
<httpModules>
<add name="HttpModule" type="ThreeHegemony.Utility.AutoAddCid"/>
</httpModules>
2)通過繼承IHttpModule來實(shí)現(xiàn)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地址后自動(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;
}
}
}
}
- ASP.NET Core 1.0 部署 HTTPS(.NET Core 1.0)
- 運(yùn)行asp.net時(shí)出現(xiàn) http錯(cuò)誤404-文件或目錄未找到
- Asp.net XMLHTTP封裝類(GET,Post發(fā)送和接收數(shù)據(jù))
- asp.net HttpWebRequest自動(dòng)識(shí)別網(wǎng)頁編碼
- asp.net 模擬提交有文件上傳的表單(通過http模擬上傳文件)
- Javascript+XMLHttpRequest+asp.net無刷新讀取數(shù)據(jù)庫數(shù)據(jù)
- asp.net利用HttpModule實(shí)現(xiàn)防sql注入
- asp.net 客戶端瀏覽器緩存的Http頭介紹
- ASP.NET MVC Web API HttpClient簡介
- ASP.NET Core Kestrel 中使用 HTTPS (SSL)
相關(guān)文章
.Net基于MVC4 Web Api輸出Json格式實(shí)例
這篇文章主要介紹了.Net基于MVC4 Web Api輸出Json格式的實(shí)現(xiàn)方法,實(shí)例講述了Global中json的操作與XML的處理等技巧,需要的朋友可以參考下2014-10-10asp.net SqlParameter如何根據(jù)條件有選擇的添加參數(shù)
有時(shí)候?qū)憇ql語句的時(shí)候會(huì)根據(jù)方法傳進(jìn)來的參數(shù)來判斷sql語句中where條件的參數(shù),下面有個(gè)示例,大家可以參考下2014-06-06ASP.NET?Core中Cookie驗(yàn)證身份用法詳解
本文詳細(xì)講解了ASP.NET?Core中Cookie驗(yàn)證身份的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02ASP.NET Core依賴注入系列教程之控制反轉(zhuǎn)(IoC)
這篇文章主要給大家介紹了關(guān)于ASP.NET Core依賴注入系列教程之控制反轉(zhuǎn)(IoC)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11Asp.net 2.0 無刷新圖片上傳 顯示縮略圖 具體實(shí)現(xiàn)
簡單三步實(shí)現(xiàn)圖片無刷新上傳:注意是上傳,至于上傳時(shí)的驗(yàn)證,比如圖片的尺寸,大小,格式。自行解決。如果我搞定了,也會(huì)貼上來的。2013-06-06asp.net高效替換大容量字符實(shí)現(xiàn)代碼
每次替換完后,在下次替換時(shí)先排除這次替換的內(nèi)容,累加本次替換的內(nèi)容。2008-08-08Visual Studio 2017正式版發(fā)布 亮點(diǎn)看這里
終于等到你,最強(qiáng) IDE Visual Studio 2017 正式版發(fā)布,這篇文章主要為大家詳細(xì)解析了Visual Studio 2017正式版發(fā)布的細(xì)節(jié),亮點(diǎn)看這里2017-03-03一個(gè)比較通用的分頁控件,完整的設(shè)計(jì)時(shí)支持和比較流行的分頁模式(提供源碼下載)
本分頁控件還包含簡單屬性,復(fù)雜屬性,自定義視圖狀態(tài),分頁事件,創(chuàng)建控件,render控件,Attribute,設(shè)計(jì)時(shí)支持等比較齊全的自定義控件的元素,是個(gè)不錯(cuò)學(xué)習(xí)自定義控件開發(fā)的例子2010-12-12asp.net 購物車實(shí)現(xiàn)詳細(xì)代碼
asp.net 購物車實(shí)現(xiàn)詳細(xì)代碼2008-11-11