【发布时间】:2022-01-16 22:30:51
【问题描述】:
这是我的 Global.asax
public void GetRewrites()
{
if (rewrites == null)
rewrites = Rewrite.getRules(); //reads from the DB
}
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Clear();
RouteTable.Routes.EnableFriendlyUrls();
RouteTable.Routes.MapPageRoute("", "home", "~/Default.aspx");
RouteTable.Routes.MapPageRoute("", "login", "~/Login.aspx");
...others
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
String fullOriginalPath = Request.Url.ToString();
//if link contains .aspx is not a rewrite
if(fullOriginalPath.Contains(".aspx"))
{
return;
}
GetRewrites();
int index = 0;
if (fullOriginalPath.Contains("www.mysite.com"))
index = fullOriginalPath.IndexOf('/', fullOriginalPath.IndexOf("www.mysite.com")) + 1;
else
index = fullOriginalPath.IndexOf('/', fullOriginalPath.IndexOf("localhost")) + 1;
string chiave = fullOriginalPath.Substring(index).ToLower();
//check if exists in RouteTable
//skip first 2 element
for (int i = 2; i< RouteTable.Routes.Count; i++)
{
System.Web.Routing.Route rx = RouteTable.Routes[i] as System.Web.Routing.Route;
if (rx.Url == chiave)
return;
}
//if not exists in redirect rules it must go to the home page
if(rewrites.Where(x=>x.Chiave == chiave).Count() == 0)
{
Response.Redirect("home");
return;
}
}
问题来了:
如果我去 localhost:4210/home 就可以了
如果我在服务器上发布我的应用程序并尝试访问链接 www.mysite.com/home
我得到了错误 网站重定向次数过多。
有人可以对动机有一个想法吗?
【问题讨论】:
-
请问,您能否添加有关返回的错误和浏览器控制台的更多详细信息?
-
我认为问题在于您正在计算“索引”。对于发布代码时,您应该在末尾删除“+1”,因为它在“/”字符所在位置之后开始新字符串(它适用于 localhost,因为在“/”之前还有端口) .
标签: asp.net url-rewriting global-asax