How can I get the baseurl of site?

后端 未结 12 1632
借酒劲吻你
借酒劲吻你 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条回答
  •  北海茫月
    2020-11-30 17:53

    I believe that the answers above doesn't consider when the site is not in the root of the website.

    This is a for WebApi controller:

    string baseUrl = (Url.Request.RequestUri.GetComponents(
                        UriComponents.SchemeAndServer, UriFormat.Unescaped).TrimEnd('/') 
                     + HttpContext.Current.Request.ApplicationPath).TrimEnd('/') ;
    

提交回复
热议问题