現(xiàn)在我們團(tuán)隊(duì)在開(kāi)發(fā)手機(jī)站,當(dāng)中遇到一個(gè)問(wèn)題.就是每張頁(yè)面得傳一個(gè)參數(shù),而這個(gè)參數(shù)又不是從固定的頁(yè)面進(jìn)來(lái)的,也就是說(shuō)無(wú)論從哪張頁(yè)面進(jìn)來(lái),參數(shù)都要全站點(diǎn)的頁(yè)面?zhèn)飨氯?目前找不到解決辦法.從網(wǎng)上down了一篇代碼,可是RegexResource.ACTION,RegexResource.HREF報(bào)錯(cuò),麻煩高手幫忙解決一下.下面我把代碼貼上,小弟跪謝...
?
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;??????????? }??????? }??? }}
8 回答

千巷貓影
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
無(wú)論從哪張頁(yè)面進(jìn)來(lái),參數(shù)都要全站點(diǎn)的頁(yè)面?zhèn)飨氯?/p>
?
既然是這樣的話樓主為什么非要使用url傳參來(lái)實(shí)現(xiàn)呢,定義全局變量,或者使用Session來(lái)存儲(chǔ)要傳遞的參數(shù)豈不是更好?

慕容708150
TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
手機(jī)站點(diǎn)對(duì)于session來(lái)講不適合,因?yàn)橛袝r(shí)候會(huì)沒(méi)有信號(hào),這樣session就中斷了.

MYYA
TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
由于我們這個(gè)項(xiàng)目是用于手機(jī)的,如果用session的話是可以保存參數(shù)的值,但是用戶保存的頁(yè)面的路徑中沒(méi)有這個(gè)參數(shù)的話,他下一次用這個(gè)路徑來(lái)登的話那我們就永遠(yuǎn)取不到值的.

楊魅力
TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
根據(jù)你的需求
目前用Global.cs處理相對(duì)簡(jiǎn)單,引入也方便,具體步驟和原理和和httpmodule一致如下:
1:截取輸出前的內(nèi)容文本
2:正則取出所有href的鏈接
3:循環(huán)替換href的鏈接
4:輸出到客戶端
這里有兩篇文章:是處理頁(yè)面標(biāo)題的,不過(guò)方式是一致的,只是需要處理的正則需要發(fā)現(xiàn)點(diǎn)變化:
- 8 回答
- 0 關(guān)注
- 468 瀏覽
添加回答
舉報(bào)
0/150
提交
取消