How to find base url from dll in c #?

From the DLL that is called by the C # .NET web application, how do you find the base URL of the web application?

+3
source share
5 answers

Will this work?

HttpContext.Current.Request.Url

UPDATE:

To get the base url you can use:

HttpContext.Current.Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped)
+12
source

If this is an assembly that non-web projects can reference, you may not need the System.Web namespace.

I would use the DannySmurf method.

+1
source

, HttpContext.Current.Request.Url, http://:

HttpContext.Current.Request.Url.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped);
+1

Assembly.GetExecutingAssembly(), DLL.

Server.MapPath, FullPath , .

0

, , :

string _baseUrl = String.Empty;
HttpContext httpContext = HttpContext.Current;
if (httpContext != null)
{
    _baseURL = "http://" + HttpContext.Current.Request.Url.Host;
    if (!HttpContext.Current.Request.Url.IsDefaultPort)
    {
        _baseURL += ":" + HttpContext.Current.Request.Url.Port;
    }
}
0

Source: https://habr.com/ru/post/1698192/


All Articles