首页 新闻 会员 周边

如何利用HttpModule给Url追加参数?

0
悬赏园豆:200 [已解决问题] 解决于 2010-12-06 09:45

现在我们团队在开发手机站,当中遇到一个问题.就是每张页面得传一个参数,而这个参数又不是从固定的页面进来的,也就是说无论从哪张页面进来,参数都要全站点的页面传下去,目前找不到解决办法.从网上down了一篇代码,可是RegexResource.ACTION,RegexResource.HREF报错,麻烦高手帮忙解决一下.下面我把代码贴上,小弟跪谢...

 

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, "&amp;cid=" + HttpContext.Current.Request["cid"]);
                }

                return result;
            }
        }
    }
}

万法归一的主页 万法归一 | 初学一级 | 园豆:57
提问于:2010-12-02 16:19
< >
分享
最佳答案
0

看了你的代码,无非就是想在输出html时把所有的链接地址加上一个参数而已,那你可以研究一下怎么改写输出的流,其实你上面的那段代码也是改写输出流,之前写过一篇文章,你可以参考下.http://www.cnblogs.com/robot/archive/2008/07/22/1248679.html

收获园豆:200
I,Robot | 大侠五级 |园豆:9783 | 2010-12-02 22:25
问题解决了,谢谢你的回答,分就给你了
万法归一 | 园豆:57 (初学一级) | 2010-12-06 09:42

@万法归一: 兄弟,你是怎么解决的啊,求方法!

云软科技 | 园豆:200 (初学一级) | 2014-03-15 11:59
其他回答(4)
0
artwl | 园豆:16736 (专家六级) | 2010-12-02 16:53
0

无论从哪张页面进来,参数都要全站点的页面传下去

 

既然是这样的话楼主为什么非要使用url传参来实现呢,定义全局变量,或者使用Session来存储要传递的参数岂不是更好?

雪莱·亨尼格 | 园豆:524 (小虾三级) | 2010-12-02 16:58
手机站点对于session来讲不适合,因为有时候会没有信号,这样session就中断了.
支持(0) 反对(0) 万法归一 | 园豆:57 (初学一级) | 2010-12-02 17:11
由于我们这个项目是用于手机的,如果用session的话是可以保存参数的值,但是用户保存的页面的路径中没有这个参数的话,他下一次用这个路径来登的话那我们就永远取不到值的.
支持(0) 反对(0) 万法归一 | 园豆:57 (初学一级) | 2010-12-02 17:24
0

不是很懂,cookie可以满足要求吗?

沉默的糕点 | 园豆:1786 (小虾三级) | 2010-12-03 00:18
cookie 是不行的
支持(0) 反对(0) 万法归一 | 园豆:57 (初学一级) | 2010-12-03 09:08
0

根据你的需求

目前用Global.cs处理相对简单,引入也方便,具体步骤和原理和和httpmodule一致如下:

1:截取输出前的内容文本

2:正则取出所有href的链接

3:循环替换href的链接

4:输出到客户端

这里有两篇文章:是处理页面标题的,不过方式是一致的,只是需要处理的正则需要发现点变化:

1:一个页面标题和过滤输出的解决方案(上)

2:一个页面标题和过滤输出的解决方案(下)

路过秋天 | 园豆:4787 (老鸟四级) | 2010-12-03 08:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册