C# .net Web 应用程序重定向中的问题

发布于 2024-11-25 02:05:55 字数 2006 浏览 0 评论 0原文

我编写了这个 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() { }

        }
    }

I coded this httpmodule and added it correctly to the site but it give me this error when I run it:

**The page isn't redirecting properly

Firefox has detected that the server is redirecting the
request for this address in a way that will never complete.**

    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() { }

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

伤感在游骋 2024-12-02 02:05:55

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

Every begin request is redirecting, even to the same url. You need a check to make sure that redirection is necessary before calling context.Response.Redirect().

浮光之海 2024-12-02 02:05:55

我认为问题在于:

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

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

I think the problem is in:

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

BeginRequest event signals the creation of any given new request. So in every redirect, it's getting called. And in your code, it's redirected to a new request which causes an infinite loop. Try to reconsider your logic.

十六岁半 2024-12-02 02:05:55

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

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

In addition to the recusion problem listed in other answers, it looks like you are redirecting to a relative path (www.example.com/Blog/?tag=/....)

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

も星光 2024-12-02 02:05:55

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();
}

In application_BeginRequest you are redirecting every request via context.Response.Redirect(ParseAndReapply(req));

You should check if a condition is true before redirecting, such as

string req = context.Request.FilePath;
if (req.Contains(".") && req.Contains("example.com"))
{
    context.Response.Redirect(ParseAndReapply(req))
    context.Response.End();
}
陌伤浅笑 2024-12-02 02:05:55

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

另一项注释:

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

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

If the argument for ParseAndReply doesn't contain "example.com", it will infinitely redirect onto itself.

One other note:

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

is redundant. "example.com" will always contain '.'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文