C# .net Web 应用程序重定向中的问题
我编写了这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
每个开始请求都会重定向,甚至重定向到相同的 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()
.我认为问题在于:
BeginRequest 事件表示任何给定新请求的创建。所以在每个重定向中,它都会被调用。在您的代码中,它被重定向到一个新请求,这会导致无限循环。尝试重新考虑你的逻辑。
I think the problem is in:
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.
除了其他答案中列出的回避问题之外,您似乎正在重定向到相对路径(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=/....
在
application_BeginRequest
中,您通过context.Response.Redirect(ParseAndReapply(req)); 重定向每个请求;
您应该在重定向之前检查条件是否为真,例如
In
application_BeginRequest
you are redirecting every request viacontext.Response.Redirect(ParseAndReapply(req));
You should check if a condition is true before redirecting, such as
如果 ParseAndReply 的参数不包含“example.com”,它将无限重定向到自身。
另一项注释:
是多余的。 “example.com”将始终包含“.”
If the argument for ParseAndReply doesn't contain "example.com", it will infinitely redirect onto itself.
One other note:
is redundant. "example.com" will always contain '.'