However, the mobile client does not support transferring values through sessions and cookies. It is too difficult to assign values to pages and then transfer values through other methods. In addition, it is not easy to maintain and error-prone to loss, so I thought of using HttpModule to assign the cid parameter to the Url address, so that the url will automatically pass the cid parameter on each page. When using the cid, I only need to obtain it through Requet ["cid, in this way, you don't have to worry about passing values.
The configuration method and Source Code are as follows.
1) Add the following nodes to the web. config configuration file:Copy codeThe Code is as follows: <Add name = "HttpModule" type = "ThreeHegemony. Utility. AutoAddCid"/>
</HttpModules>
2) inherit IHttpModule to implement url-based value transfer.
CodeCopy codeThe Code is as follows: 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: This class is automatically added after the Url address (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;
}
}
}
}