Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析.NET中的部分URL_C#_F#_Uri - Fatal编程技术网

C# 解析.NET中的部分URL

C# 解析.NET中的部分URL,c#,f#,uri,C#,F#,Uri,假设我有一些来自网页的链接https://example.com/xyz 像这样: /abc 另一个网站 def 鉴于网页的URL,我想修复这些链接: /abc->https://example.com/abc 另一个网站->https://another.com def->https://example.com/xyzdef 在.NET Core C/F中如何执行此操作?Uri.TryCreate方法以您想要的方式处理三种情况中的两种: let ok, url = Uri.TryCreate(

假设我有一些来自网页的链接https://example.com/xyz 像这样:

/abc 另一个网站 def 鉴于网页的URL,我想修复这些链接:

/abc->https://example.com/abc 另一个网站->https://another.com def->https://example.com/xyzdef 在.NET Core C/F中如何执行此操作?

Uri.TryCreate方法以您想要的方式处理三种情况中的两种:

let ok, url = Uri.TryCreate(Uri("https://example.com/xyz"), "/abc")
// https://example.com/abc

let ok, url = Uri.TryCreate(Uri("https://example.com/xyz"), "another.com")
// https://example.com/another.com

let ok, url = Uri.TryCreate(Uri("https://example.com/xyz"), "#def")
// https://example.com/xyz#def
在第二种情况下,另一个.com只是被解释为一个文件名,而不是域名。这是很合理的,因为这个案子模棱两可

我认为没有一种方法可以使用内置的.NET方法自动处理第二种情况。您可能需要自己实现该逻辑。这取决于你到底想要什么——如果你想转身的话。在https://中,您可以检查字符串是否匹配。模式,并确保其有效,以避免将index.html转换为https://index.html 然后自己做这个转换