Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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# 如何检测URL的特定部分并对其进行修改?_C#_Asp.net - Fatal编程技术网

C# 如何检测URL的特定部分并对其进行修改?

C# 如何检测URL的特定部分并对其进行修改?,c#,asp.net,C#,Asp.net,我正在使用asp.net开发网站。在这里,我主要使用URL来传递参数 我有这样的URL结构 http://localhost:51247/yyy/zzz/hrforum/(if its in my local PC) http://test.com/yyy/zzz/hrforum/ 我需要检测zzz部分并用另一个单词替换它。我尝试了很多东西,包括正则表达式模式,但似乎我做git的方式不对。请帮我检测一下。修改它并重新生成URL 我试过的密码 Regex myRegex = new Regex

我正在使用asp.net开发网站。在这里,我主要使用URL来传递参数

我有这样的URL结构

http://localhost:51247/yyy/zzz/hrforum/(if its in my local PC)
http://test.com/yyy/zzz/hrforum/
我需要检测zzz部分并用另一个单词替换它。我尝试了很多东西,包括正则表达式模式,但似乎我做git的方式不对。请帮我检测一下。修改它并重新生成URL

我试过的密码

 Regex myRegex = new Regex(@"/([\w\s]+?\;){2}/");
    var match = myRegex.Match(fullUrl);
    var firstName = match.Groups[0].Value;

但这不起作用。

我不确定你想要实现什么,但要回答这个问题:

如何检测URL的特定部分并对其进行修改

我认为可以使用class而不是Regex

var uri=新uri(“http://test.com/yyy/zzz/hrforum/");
var pathName=uri.PathAndQuery;
foreach(路径名.Split('/')中的变量项)
{
控制台写入线(项目);
}
//输出:
//yyy
//zzz
//人力资源论坛

最简单的方法是使用属性。例如:

    Uri uriAddress1 = new Uri("http://test.com/yyy/zzz/hrforum/");
    Uri uriAddress2 = new Uri("ttp://localhost:51247/yyy/zzz/hrforum/");
    
    Console.WriteLine(uriAddress1.Segments[2] == uriAddress2.Segments[2]);
    Console.WriteLine("Segment 2 of Address 1: {0} Segment 2 of Address 2: {1}", uriAddress1.Segments[2].Trim('/'),uriAddress2.Segments[2].Trim('/'));
输出:

真的

地址1第2段:zzz地址2第2段:zzz


你为什么要这么做?这是用于路由还是其他?请注意,“代码”一词用作“质量名词”,意思是它不需要“s”或“z”