<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

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

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:43:32
        文檔

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

        asp.net通過HttpModule自動在Url地址上添加參數(shù):然而手機客戶端又不支持Session和Cookie傳值,其他方法給頁面賦值再傳值顯得太麻煩了,而且還不易維護,容易弄丟出錯,于是想到了用HttpModule來把cid參數(shù)賦在Url地址上,讓url把cid參數(shù)每頁自動傳遞下去,需要用cid時只要通過Requet[cid]獲取
        推薦度:
        導(dǎo)讀asp.net通過HttpModule自動在Url地址上添加參數(shù):然而手機客戶端又不支持Session和Cookie傳值,其他方法給頁面賦值再傳值顯得太麻煩了,而且還不易維護,容易弄丟出錯,于是想到了用HttpModule來把cid參數(shù)賦在Url地址上,讓url把cid參數(shù)每頁自動傳遞下去,需要用cid時只要通過Requet[cid]獲取

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

        1)在web.config配置文件中添加以下節(jié)點
        代碼如下:


        <httpModules>
        <add name="HttpModule" type="ThreeHegemony.Utility.AutoAddCid"/>
        </httpModules>

        2)通過繼承IHttpModule來實現(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地址后自動添加(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;
        }
        }
        }
        }

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

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

        asp.net通過HttpModule自動在Url地址上添加參數(shù):然而手機客戶端又不支持Session和Cookie傳值,其他方法給頁面賦值再傳值顯得太麻煩了,而且還不易維護,容易弄丟出錯,于是想到了用HttpModule來把cid參數(shù)賦在Url地址上,讓url把cid參數(shù)每頁自動傳遞下去,需要用cid時只要通過Requet[cid]獲取
        推薦度:
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲精品视频在线观看免费| 国产在线观看免费完整版中文版| 亚洲国产精品无码成人片久久 | 亚洲精品动漫人成3d在线 | 国产又长又粗又爽免费视频| 亚洲AV无码久久久久网站蜜桃| 最近2018中文字幕免费视频| 久久久久亚洲av无码专区喷水| 99国产精品视频免费观看| 亚洲天堂视频在线观看| 91久久精品国产免费一区| 亚洲成人黄色在线| 免费一本色道久久一区| 亚洲色大成网站www尤物| 国产精品va无码免费麻豆| 国产成人不卡亚洲精品91| 亚洲乱码中文字幕手机在线| 国产免费人成视频在线播放播 | 亚洲成av人在线观看网站| 国产99视频免费精品是看6| 又长又大又粗又硬3p免费视频| 亚洲熟妇无码另类久久久| 男人都懂www深夜免费网站| 亚洲人成高清在线播放| 国产免费久久精品| 中文成人久久久久影院免费观看 | 99热免费在线观看| 亚洲一区精彩视频| 亚洲av日韩片在线观看| 黄网站免费在线观看| 亚洲1区1区3区4区产品乱码芒果| 国产精品冒白浆免费视频 | 久久久久久久久免费看无码| 亚洲avav天堂av在线网毛片| 亚洲深深色噜噜狠狠爱网站| 成人在线免费看片| 一区二区三区免费在线视频 | 亚洲国产精品ⅴa在线观看| 国产亚洲精品AA片在线观看不加载| 91香蕉国产线在线观看免费| 国产av无码专区亚洲av毛片搜|