开发者

How to get the site address without subfolders?

开发者 https://www.devze.com 2023-03-21 11:05 出处:网络
I want to get the website url without subfolders, I mean everything replaced by ~ For example: http://www.myWebAp开发者_运维百科plication.com/a

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

精彩评论

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