Get base URL of an ASP.NET MVC Application

Get base URL of an ASP.NET MVC Application

Hello everyone, I am going to share the code sample to get the base URL using ASP.Net MVC and the code detail as given below.

Please see the example and put your thoughts and comment as well.

public class ApplicationConfig
{
        public static string GetBaseUrlByKey(string key)
        {
            if (key == Constants.baseURL)
            {
                return GetBaseUrl();
            }
        }

        public static string GetBaseUrl()
        {
            var requestURL = HttpContext.Current.Request;
            var appDomainUrl = HttpRuntime.AppDomainAppVirtualPath;

            if (!string.IsNullOrWhiteSpace(appDomainUrl)) appDomainUrl += "/";


            var baseURL = string.Format("{0}://{1}{2}", requestURL.Url.Scheme, requestURL.Url.Authority, appDomainUrl);
            return baseURL;
        }
 }


Thank you!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.