c# - 检查字符串中是否为根域

标签 c# .net

我是 C# 新手,

假设我有一个字符串

string testurl = "http://www.mytestsite.com/hello";


if (test url == root domain) {

// do something
}

我想检查该字符串“testurl”是否是根域,即 http://www.mytestsite.comhttp://mytestsite.com等等

谢谢。

最佳答案

使用Uri类:

var testUrl = new Uri("http://www.mytestsite.com/hello");


if (testUrl.AbsolutePath== "/")
{
    Console.WriteLine("At root");
}
else
{
    Console.WriteLine("Not at root");
}

它很好地处理了可能需要的任何规范化问题(例如处理 http://www.mytestsite.comhttp://www.mytestsite.com/ 相同)

关于c# - 检查字符串中是否为根域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22859924/

相关文章:

c# - 将 C# 程序部署到 Windows Mobile 6.5 设备时出现 "Reference package not found. Device Connectivity Component"

c# - 当我模拟我的 ASP.NET MVC Controller 时,我的 ActionMethod 不返回任何 View 。为什么?

c# - 将sql命令写入文本文件

c# - 在 C# 中将 Excel 列(或单元格)格式化为文本?

c# - 如何在散点图中绘制超过 50,000 个值,从而节省计算机资源?

c# - StringTemplate:从磁盘加载模板?

c# - 有没有办法可以延迟LINQ语句中的.Where子句?

.net - 有没有类似于 django admin 的开源项目,但是是.NET 的?

c# - 使用客户端自定义资源 (resx) 以及默认资源

C# Surface Pro 4 相机