清洁/格式的url
本文关键字:url 格式 清洁 | 更新日期: 2023-09-27 18:05:36
如果我有以下URL:
-
/网站/testsite/子/共享% 20 documents1/项目/项目% 20 - % 20 csf % 20医疗% 20病人% 20餐饮发展% 20
-
/网站/testsite/subsite2/医疗/sd/文件/清洁% 20服务
我需要能够清理url,所以我这样做:
string webUrl = sd.Key.Substring(0, sd.Key.ToLower().IndexOf("documents") - 1);
这对于2秒的链接非常有效,它给了我以下清理后的URL:
/网站/testsite/subsite2/医疗/sd
然而,这不是通用的,它不适用于第一个Url,我得到的是以下内容:
/网站/testsite/子/共享% 2
理想情况下,我想在这里得到的是
/网站/testsite/子
是否有更好的方法(通用)来确保这对两个url都有效?
这些是转义字符串,使用javascript函数unescape()来反转义。
。
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
并使用HttpUtility。c#中的HtmlDecode
var result = HttpUtility.HtmlDecode("/sites/testsite/subsite/shared%20documents1/projects/project%20-%20csf%20healthcare%20patient%20dining%20development");
最好的方法是使用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
则可以删除空格。有关更多信息,请使用此链接
如果你想检索"/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"
或者为了检索/*documents中剩下的所有内容
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