asp.net通過HttpModule自動在Url地址上添加參數

來源:互聯網
上載者:User

然而手機用戶端又不支援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;
}
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.