清理/格式化网址

时间:2014-05-28 09:12:24

标签: c# sharepoint

如果我有以下网址:

  1. /位点/测试网站/子网站/共享%20documents1 /项目/项目%20-%20csf%20healthcare%20patient%20dining%20development

  2. /位点/测试网站/ subsite2 /医疗/ SD /文档/清洁%20Services

  3. 我需要能够清理网址,以便我执行以下操作:

    string webUrl = sd.Key.Substring(0, sd.Key.ToLower().IndexOf("documents") - 1);
    

    这适用于2秒链接,它为我提供了以下清理的URL:

      

    / sites / testsite / subsite2 / healthcare / sd

    然而,这不是通用的,它不适用于第一个Url,我得到的是以下内容:

      

    /位点/测试网站/子网站/共享%2

    理想情况下,我想要的是

      

    /位点/测试网站/亚位点

    是否有更好的方法(通用)来确保这适用于两个网址?

3 个答案:

答案 0 :(得分:1)

这些是转义字符串,使用javascript函数unescape()来取消它们。

e.g。

unescape('/sites/testsite/subsite/shared%20documents1/projects/project%20-%20csf%20healthcare%20patient%20dining%20development')

//sites/testsite/subsite/shared documents1/projects/project - csf healthcare patient dining development

在C#中使用HttpUtility.HtmlDecode

var result = HttpUtility.HtmlDecode("/sites/testsite/subsite/shared%20documents1/projects/project%20-%20csf%20healthcare%20patient%20dining%20development");

答案 1 :(得分:0)

执行此操作的最佳方法是使用Uri.UnescapeDataString

 Uri.UnescapeDataString(@"/sites/testsite/subsite/shared%20documents1/projects/project%20-%20csf%20healthcare%20patient%20dining%20development")

这会给你

/sites/testsite/subsite/shared documents1/projects/project - csf healthcare patient dining development

然后你可以删除空格。有关更多信息,请使用此link

答案 2 :(得分:0)

如果您尝试仅检索" / sites / sitename / subsite"你可以这样做

var match = Regex.Match("/sites/compass/community/Shared%20documents1/projects/project%20-%20csf%20healthcare%20patient%20dining%20development", "^/sites/.*?/.*?/", RegexOptions.IgnoreCase);

if (match.Success)
    Console.WriteLine(match.Value) // "/sites/compass/community"

或者为了从/ *文件

中检索所有内容
var source = new [] {"/sites/testsite/subsite/shared%20documents1/projects/project%20-%20csf%20healthcare%20patient%20dining%20development", 
        "/sites/testsite/subsite2/healthcare/sd/Documents/Cleaning%20Services"};

foreach (var item in source)
{
    var match = Regex.Match(item, "^(.*)(?:/.*?Documents)", RegexOptions.IgnoreCase);

    if (match.Success)
        Console.WriteLine(match.Groups[1].Value);
}

输出:

/sites/testsite/subsite
/sites/testsite/subsite2/healthcare/sd