How can I get the baseurl of site?

后端 未结 12 1562
借酒劲吻你
借酒劲吻你 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 18:11

    Based on what Warlock wrote, I found that the virtual path root is needed if you aren't hosted at the root of your web. (This works for MVC Web API controllers)

    String baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority) 
    + Configuration.VirtualPathRoot;
    

提交回复
热议问题