开发者

How to get the site address without subfolders?

I want to get the website url without subfolders, I mean everything replaced by ~

For example:

http://www.myWebAp开发者_运维百科plication.com/a

http://www.myWebApplication.com?somepage=b

I just want: http://www.myWebApplication.com

http://www.myWebApplication:3345/c

I just want: http://www.myWebApplication:3345

Any help!


var url = new Uri("http://www.myWebApplication:3345/c");
var siteAddress = url.Scheme + Uri.SchemeDelimiter + url.Host + 
    (url.IsDefaultPort? "":(":" + url.Port));

If it's the Request url, you can get it from Request.Url instead of building a new Uri.


var uriBuilder = new System.UriBuilder
    {
        Host = HttpContext.Request.Url.Host, 
        Path = "/",
        Port = HttpContext.Request.Url.Port,
        Scheme = HttpContext.Request.Url.Scheme
    };

var websiteUrl = uriBuilder.ToString();


Just use HttpContext.Current.Request.Url.Host

Edit: You can play around HttpContext.Current.Request.Url to get most of the information you need from the URL

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜