我的global.asax文件中有很少的重定向定义。一个例子如下。
所有重定向都能正常工作,但是当我尝试添加新的重定向时,这是第一个在下面示例中定义的重定向,但由于某种原因它无法正常工作。
我在浏览器http://website.com/Page.aspx?PageId=15
//This doesn't work
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.com/Page.aspx?PageId=15"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.com/Page.aspx?PageId=15", "http://website.com/contact.aspx?PageId=15&Language=en-US"));
}
//This work s
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.com/news"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.com/news", "http://website.com/news.aspx?PageId=25&Language=en-US"));
}
其他网址正确重定向,如http://website.com/news
http://website.com/about-us
等。
答案 0 :(得分:3)
您已将自己的网址设置为ToLower,但是您要将其与使用大写字母的网址进行比较。
尝试设置您将Url与小写字母进行比较的字符串文字:
Request.Url.ToString().ToLower().Contains("http://website.com/page.aspx?pageid=15")
答案 1 :(得分:0)
使用else if
代替两个if
条件。