2014-09-30 56 views
0

我在我的global.asax文件中定義了一些重定向。一個例子如下。301重定向在Global.asax文件中無法正常工作

所有的重定向工作正常,但是當我試圖添加一個新的重定向,這是第一個定義在下面的例子中,它不工作的原因。

我在瀏覽器中輸入一個URL樣本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")); 
    } 

其他URL重定向正確像http://website.com/newshttp://website.com/about-us等。

+0

你的意思是不工作的地方目前重定向? – 2014-09-30 12:25:50

+0

我的意思是如果我輸入url'http://website.com/Page.aspx?PageId = 15',那麼它應該重定向到'http://website.com/contact.aspx?PageId = 15&Language = en-US'而只是執行沒有重定向輸入的URL ...'http://website.com/Page.aspx?PageId = 15' – Learning 2014-09-30 12:29:09

+0

確保字符串存在完整的url,否則它應該工作 – 2014-09-30 12:34:22

回答

3

你的URL設置與tolower,但你要比較它到大寫字母的網址。

嘗試設置字符串文字,你用小寫字母比較您的網址,以及:

Request.Url.ToString().ToLower().Contains("http://website.com/page.aspx?pageid=15") 
+0

沒有得到你的觀點,我正在使用Request.Url.ToString()。ToLower()'這兩個語句。我正在使用.'ToLower()'作爲使用可以在任何形式的小型,帽子,sentenceCase等在URL中鍵入。 – Learning 2014-09-30 12:38:32

+0

你誤會了我。你比較你的Url的字符串有大寫字母。包含區分大小寫。您正在設置您的URL ToLower(),但您將它與大寫字母的內容進行比較。因此,您的Url會嘗試將page.aspx與Page.aspx進行比較,但由於它是區分大小寫的比較,因此無法匹配它們,因爲您的P在兩個字符串中都是不同的情況。 – Nzall 2014-09-30 12:39:52

+1

+1。 @KnowledgeSeeker你正在使用ToLower,它會將URL小寫,但你將它與一個不低於loweracse的字符串進行比較,所以它永遠不會是真的。第二條語句的作用是因爲您將它與全小寫字符串進行比較。 – MikeSmithDev 2014-09-30 12:41:10

0

使用else if而不是兩個if條件。