本文介绍了确定绝对或相对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在字符串中有一个相对或绝对URL.我首先需要知道它是绝对的还是相对的.我该怎么做呢?然后,我想确定url的域是否在允许列表中.
I have a relative or absolute url in a string. I first need to know whether it is absolute or relative. How do I do this? I then want to determine if the domain of the url is in an allow list.
这是我的允许列表,例如:
Here is my allow list, as an example:
string[] Allowed =
{
"google.com",
"yahoo.com",
"espn.com"
}
一旦我知道它是相对的还是绝对的,我就认为它相当简单:
Once I know whether its relative or absolute, its fairly simple I think:
if (Url.IsAbsolute)
{
if (!Url.Contains("://"))
Url = "http://" + Url;
return Allowed.Contains(new Uri(Url).Host);
}
else //Is Relative
{
return true;
}
推荐答案
bool IsAbsoluteUrl(string url)
{
Uri result;
return Uri.TryCreate(url, UriKind.Absolute, out result);
}
这篇关于确定绝对或相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!