1

It is shocking how something as simple as getting the App baseURL seems to be hard to come by

Basically I want a method to return http://localhost:2000/ if I am running the app locally or http://myapp.azurewebsites.com/ if it's remote.

I followed How to get base url of the site in MVC with literally conflicting results.

Community
  • 1
  • 1
PHPer
  • 637
  • 4
  • 19

1 Answers1

0

I came across this one and it seems to work as expected. I will make a repo and include it with a few more help methods that can be useful.

    public string Root
    {
        get
        {
            var context = HttpContext.Current;
            if (context != null)
            {
                var request = context.Request;
                var scheme = request.Url.Scheme;
                var server = request.Headers["Host"] ?? string.Format("{0}:{1}", request.Url.Host, request.Url.Port);
                var host = string.Format("{0}://{1}", scheme, server);
                var root = host + VirtualPathUtility.ToAbsolute("~");
                return root;
            }
            return string.Empty;
        }
    }
    public string getBaseUrl()
    {
        string baseURL = HttpContext.Current.Request.Url.Host;
        return Root;
    }
PHPer
  • 637
  • 4
  • 19