我正在尝试验证表示网址的字符串是相对的,但似乎Uri.TryCreate()
将接受任何不具有相关协议的网址(即:www.domain.com/something/else.aspx )。但我只需要/something/else.aspx /
例如,尝试在第9行获取uri.PathAndQuery
时,此测试字符串将始终抛出异常。即使TryCreate()
返回true。
string test = "/something/else.aspx";
bool isValid;
var url = getPathAndQuery(test, out isValid);
private string getPathAndQuery(string url, out bool isValid) {
Uri uri;
isValid = Uri.TryCreate(url, UriKind.Relative, out uri);
try {
return isValid ? uri.PathAndQuery : string.Empty;
} catch (Exception e) {
isValid = false;
return string.Empty;
}
}
输入/输出示例:
输入:/something/else.aspx ==>输出:/something/else.aspx
输入:www.domain.com/something/else.aspx ==>输出:/something/else.aspx
输入:http://www.domain.com/something/else.aspx ==> ouput:string.Empty
考虑到网址的相对路径,我如何验证它确实是相对的,只是在用户也通过域时获取路径和查询?