通过RegEx从URL获取值
本文关键字:获取 URL RegEx 通过 | 更新日期: 2023-09-27 18:07:30
我想从URL检索值。我的URL看起来像:
http://www.test.com/questions/gradename/http://www.test.com/questions/gradename/subjectname http://www.test.com/questions/gradename/subjectname/topicname http://www.test.com/questions/gradename/subjectname/topicname/subtopicname现在我想从URL中找出gradename(它是动态值)
如果您正在寻找一个服务器端/ASP。. NET解决方案,您可以使用Url的Segments集合,例如:
var gradeName = Request.Url.Segments[2].Trim('/');
Segments是主机名后面URL部分的数组,例如:
[ "/", "questions/", "gradename/", ... ]
Regex是预优化。将字符串分割为/,取第四个值