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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Buy Books Online at Best Prices

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.