【问题标题】:Problem in C# .net web application RedirectionC# .net Web 应用程序重定向中的问题
【发布时间】:2011-07-20 19:53:22
【问题描述】:

我编写了这个 httpmodule 并将其正确添加到站点,但是当我运行它时它给了我这个错误:

**页面没有正确重定向

Firefox 检测到服务器正在重定向 以永远不会完成的方式请求此地址。**

    using System;
    using System.Web;
    using System.Net;
    using System.Text;
    using System.IO;

    namespace CommonRewriter
    {
        public class ParseUrl : IHttpModule
        {
            public ParseUrl()
            {

            }

            public String ModuleName
            {
                get { return "CommonRewriter"; }
            }

            public void Init(HttpApplication application)
            {
                application.BeginRequest += new EventHandler(application_BeginRequest);
                application.EndRequest += new EventHandler(application_EndRequest);
            }


            private string ParseAndReapply(string textToParse)
            {
                string final = null;

                if (textToParse.Contains(".") && textToParse.Contains("example.com"))
                {
                    string[] splitter = textToParse.Split('.');
                    if (splitter[0].ToLower() != "www" &&(splitter[2].ToLower()).Contains("blog"))
                    {
                        final = ("www.example.com/Blog/?tag=/" + splitter[0]);
                    }
                    else { final = textToParse; }
                }
                else { final = textToParse; }

                return final;
            }

            void application_BeginRequest(object sender, EventArgs e)
            {
                HttpApplication application = (HttpApplication)sender;
                HttpContext context = application.Context;

                string req = context.Request.FilePath;
                context.Response.Redirect(ParseAndReapply(req));
                context.Response.End();
            }


            void application_EndRequest(object sender, EventArgs e)
            {

            }

            public void Dispose() { }

        }
    }

【问题讨论】:

    标签: c# .net redirect httpmodule response.redirect


    【解决方案1】:

    每个开始请求都会重定向,甚至重定向到相同的 url。在调用 context.Response.Redirect() 之前,您需要检查以确保重定向是必要的。

    【讨论】:

      【解决方案2】:

      我认为问题出在:

       context.Response.Redirect(ParseAndReapply(req));  
      

      BeginRequest 事件表示任何给定新请求的创建。所以在每个重定向中,它都会被调用。在您的代码中,它被重定向到一个导致无限循环的新请求。试着重新考虑你的逻辑。

      【讨论】:

        【解决方案3】:

        除了其他答案中列出的回避问题外,您似乎正在重定向到相对路径(www.example.com/Blog/?tag=/....)

        试试http://www.example.com/Blog/?tag=/....

        【讨论】:

          【解决方案4】:

          application_BeginRequest 中,您正在通过context.Response.Redirect(ParseAndReapply(req)); 重定向每个 请求

          你应该在重定向之前检查一个条件是否为真,例如

          string req = context.Request.FilePath;
          if (req.Contains(".") && req.Contains("example.com"))
          {
              context.Response.Redirect(ParseAndReapply(req))
              context.Response.End();
          }
          

          【讨论】:

            【解决方案5】:

            如果 ParseAndReply 的参数不包含“example.com”,它将无限重定向到自身。

            另一个注意事项:

            if (textToParse.Contains(".") && textToParse.Contains("example.com"))
            

            是多余的。 “example.com”将始终包含“。”

            【讨论】:

            • 感谢您指出这一点。原来只是textToParse.Contains(".")我后来添加了“example.com”部分
            • 我正在处理的站点是一个开发站点,其 IP 地址而不是域名。我使用主机文件将 ip 更改为可用域。 string req = context.Request.FilePath; 是否可以返回一个用于评估的 IP 地址而不是文本域名。
            • 主机名无关紧要。 Filepath 返回 URL 的文件和路径部分,不包括主机名或任何查询字符串值。触发重定向条件的唯一方法是,如果您有一个类似“h ttp://somesite.com/example.com/file.aspx?arg=val1&arg2=val2”的 URL,FilePath 将返回“/example.com/file.xml”。 aspx”来自那个例子。
            • 这对我帮助很大。将使用context.Request.RawUrl 返回整个网址
            • 那仍然只提供路径和查询信息。您可能想要处理 Request.Url 对象。如果您需要完整的字符串,您可以调用 Request.Url.ToString() 来获取它。但看起来你无论如何都在解析它,而且 System.Uri (msdn.microsoft.com/en-us/library/system.uri.aspx) 已经为解析做了很多繁重的工作。
            猜你喜欢
            • 1970-01-01
            • 2015-07-24
            • 2017-07-02
            • 2017-08-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-06
            • 2012-08-22
            相关资源
            最近更新 更多