C# 检索域网址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/649617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve domain url
提问by user29964
I want to retrieve my domain url in asp.net.
我想在 asp.net 中检索我的域 url。
for example, if my url is:
例如,如果我的网址是:
http://www.mydomain.com/blog/currentPage.aspx?id=156
I just want the part
我只想要那部分
http://www.mydomain.com/blog/
can anyone help me out?
谁能帮我吗?
采纳答案by Canavar
you should do some string manipulation on this answer :
你应该对这个答案做一些字符串操作:
how to get url of the current page in c#
in addition take a look at segments.
另外看看细分市场。
回答by abatishchev
// Request.Uri
Uri originalUrl = new Uri("https://www.example.com/blog/currentPage.aspx?id=156");
// www.examle.com
string domain = originalUrl.Host;
// https://www.example.com
string domainUrl = String.Concat(originalUrl.Scheme, Uri.SchemeDelimiter, originalUrl.Host);
回答by Sander Versluys
You have many options:
你有很多选择:
string root = this.ResolveUrl("~")
Or
或者
Uri requestUri = Context.Request.Url;
string baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + requestUri.Host + (requestUri.IsDefaultPort ? "" : ":" + requestUri.Port);
Or
或者
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
If you want /blog appended to the last two, add
如果您希望 /blog 附加到最后两个,请添加
+ Request.ApplicationPath