How can I get the baseurl of site?

后端 未结 12 1643
借酒劲吻你
借酒劲吻你 2020-11-30 17:43

I want to write a little helper method which returns the base URL of the site. This is what I came up with:

public static string GetSiteUrl()
{
    string ur         


        
12条回答
  •  Happy的楠姐
    2020-11-30 17:45

    This works for me.

    Request.Url.OriginalString.Replace(Request.Url.PathAndQuery, "") + Request.ApplicationPath;
    
    • Request.Url.OriginalString: return the complete path same as browser showing.
    • Request.Url.PathAndQuery: return the (complete path) - (domain name + PORT).
    • Request.ApplicationPath: return "/" on hosted server and "application name" on local IIS deploy.

    So if you want to access your domain name do consider to include the application name in case of:

    1. IIS deployment
    2. If your application deployed on the sub-domain.

    ====================================

    For the dev.x.us/web

    it return this strong text

提交回复
热议问题