什麼是解析域和可選的子域,使用C#解析URL的最佳方式。該解決方案還應該與「本地主機」一起工作,以便相同的代碼可以用於本地開發和生產中。如何從使用C#的URL解析域和子域(如果存在)?
例子和預期結果:
http(s)://yahoo.com -> domain:yahoo.com | subdomain:
http(s)://test.yahoo.com -> domain:yahoo.com | subdomain:test
http(s)://test.testing.yahoo.com -> domain:yahoo.com | subdomain:test.testing
http(s)://localhost:55555 -> domain:localhost | subdomain:
http(s)://test.localhost:55555 -> domain:localhost | subdomain:test
http(s)://test.testing.localhost:55555 -> domain:localhost | subdomain:test.testing
http(s)://yahoo.com.uk -> domain:yahoo.com.uk | subdomain:
http(s)://test.yahoo.com.uk -> domain:yahoo.com.uk | subdomain:test
什麼定義了'這裏subdomain'?你的例子看起來很隨意。 – DavidG
此線程爲您的需求提供了一種可能的擴展方法: http://stackoverflow.com/questions/19734769/get-specific-subdomain-from-url-in-foo-bar-car-com – tendrel
我喜歡擴展方法。不知道它是否適用於我的本地主機場景。 DavidG提到了另一種場景,其中有多部分域可能無法使用它。我將不得不玩,看看。 – wgpubs