Saturday 19 August 2017

C#: Get the base url from http(s) request

In this tutorial I am going to write the function in c# that will get the base url from the http(s) request. 

Step 1: Add the following method in c# code file.

public static string GetBaseUrl(this HttpContext httpContext)
        {
            return $"{httpContext.Request.Url.Scheme}://{httpContext.Request.Url.DnsSafeHost}:{httpContext.Request.Url.Port}";
        }

Step 2: You can use the above method as following :

string url = HttpContext.Current.GetBaseUrl();

No comments:

Post a Comment