2015-09-17 48 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 
相关问题