2015-09-17 46 views
-1

我有一個網址,這就好比http://example.com/UK/Deal.aspx?id=322如何從URL檢索語言環境(國家)代碼?

我的目標是消除區域(國家)的部分,使它像http://example.com/Deal.aspx?id=322

由於URL可能有其他類似的格式,如:https://ssl.example.com/JP/Deal.aspx?id=735,使用「子串」功能不是一個好主意。

我能想到的是使用下面的方法將它們分開,然後再映射它們。

HttpContext.Current.Request.Url.Scheme 
HttpContext.Current.Request.Url.Host 
HttpContext.Current.Request.Url.AbsolutePath 
HttpContext.Current.Request.Url.Query 

而且,假設HttpContext.Current.Request.Url.AbsolutePath將是:

/UK/Deal.aspx?id=322 

我不知道如何處理這一點,因爲我的老闆問我要不要用「一般表達「(他認爲這將影響性能...)

除了正則表達式」,有沒有其他的方法來消除英國從它?

P.S。得到:UK部分可以是JPDE,或其他國家代碼。

順便說一句,對於美國,沒有國家代碼,將URL http://example.com/Deal.aspx?id=322

也請把這種情況考慮在內。 謝謝。

+1

你爲什麼要刪除並映射回來?是否因爲語言環境自動從您的網站訪問來自不同地區或國家的網站?如果是的話,那麼可能會有另一種解決方案。 – vendettamit

+1

Hi @vendettamit,實際上我的網站在這些日子裏增加了「多區域」,如果有額外的「國家代碼」,我的遺留網址相關方法將無法正常工作。在嘗試使用這些遺留方法之前,我嘗試從URL(帶有國家/地區代碼)中刪除「國家代碼」,並在處理後將其映射回。 – user3174976

+0

Gotach !!如果URL段在根目錄後有2個字母的ISO代碼作爲第一個ocurrance,您可以使用一個小的正則表達式來匹配。 @ user3174976看到我更新的答案。 – vendettamit

回答

0

假設您將在Url中擁有TwoLetterCountryISOName。 y您可以使用UriBuilder類從Uri刪除路徑,而不使用Regex

E.g.

var originalUri = new Uri("http://example.com/UK/Deal.aspx?id=322"); 
    if (IsLocaleEnabled(sourceUri)) 
     { 
     var builder = new UriBuilder(sourceUri); 
     builder.Path 
      = builder.Path.Replace(sourceUri.Segments[1] /* remove UK/ */, string.Empty); 
      // Construct the Uri with new path 
      Uri newUri = builder.Uri;; 
     } 

更新:

// Cache the instance for performance benefits. 
static readonly Regex regex = new Regex(@"^[aA-zZ]{2}\/$", RegexOptions.Compiled); 

/// <summary> 
/// Regex to check if Url segments have the 2 letter 
/// ISO code as first ocurrance after root 
/// </summary> 
private bool IsLocaleEnabled(Uri sourceUri) 
{ 
    // Update: Compiled regex are way much faster than using non-compiled regex. 
    return regex.IsMatch(sourceUri.Segments[1]); 
} 

對於性能好處,你必須緩存它(手段保持它在靜態只讀域)。不需要爲每個請求解析預定義的正則表達式。這樣你就可以獲得所有的性能優勢。

結果 - http://example.com/Deal.aspx?id=322

+0

Hi @vendettamit,如果我的網址是http://example.com/Deal.aspx?id=322美國(沒有國家代碼),有沒有什麼辦法可以忽略你在這個沙盤中的替換方法?謝謝。 – user3174976

+0

hmm !!如果URL指定了國家代碼,則需要額外檢查才能確定。 – vendettamit

+0

初始化構建器時,還需要提供初始路徑。否則,它默認爲一個回送URI,然後你的替換什麼也不做。 – AgapwIesu

0

這一切都取決於國家代碼是否總是具有相同的位置。如果不是,那麼需要更多關於可能格式的詳細信息..也許你可以檢查一下,如果第一個分段有兩個字符或者什麼的話,確定它確實是一個國家代碼(不確定這是否可靠)。或者你開始使用的文件名,如果它總是在格式/[optionalCountryCode]/deal.aspx?...

如何這兩種方法(在串級):

public string RemoveCountryCode() 
{ 
    Uri originalUri = new Uri("http://example.com/UK/Deal.aspx?id=322"); 
    string hostAndPort = originalUri.GetLeftPart(UriPartial.Authority); 

    // v1: if country code is always there, always has same position and always 
    // has format 'XX' this is definitely the easiest and fastest 
    string trimmedPathAndQuery = originalUri.PathAndQuery.Substring("/XX/".Length); 

    // v2: if country code is always there, always has same position but might 
    // not have a fixed format (e.g. XXX) 
    trimmedPathAndQuery = string.Join("/", originalUri.PathAndQuery.Split('/').Skip(2)); 

    // in both cases you need to join it with the authority again 
    return string.Format("{0}/{1}", hostAndPort, trimmedPathAndQuery); 
} 
0

如果AbsolutePath將始終具有格式/XX/...pagename.aspx?id=###其中XX是兩個字母國家代碼,那麼你可以刪除前3個字符。

的例子,消除了前3個字符:

var targetURL = HttpContext.Current.Request.Url.AbsolutePath.Substring(3); 

如果國家代碼可能是不同的長度,那麼你可以找到第二/字符的索引,並從那裏開始的子字符串。

var sourceURL = HttpContext.Current.Request.Url.AbsolutePath; 
var firstOccurance = sourceURL.IndexOf('/') 
var secondOccurance = sourceURL.IndexOf('/', firstOccurance); 

var targetURL = sourceURL.Substring(secondOccurance); 
0

最簡單的方法是將當作字符串,由「/」分隔符分割它,刪除第四個元素,然後再加入他們回用「/」分隔符:

string myURL = "https://ssl.example.com/JP/Deal.aspx?id=735"; 
    List<string> myURLsplit = myURL.Split('/').ToList().RemoveAt(3); 
    myURL = string.Join("/", myURLsplit); 

RESULT: https://ssl.example.com/Deal.aspx?id=735 
相關問題