Getting relative root in Global.Asax

I am trying to keep the physical root and relative root of my application in memory when the application starts.

I made the physical path as follows:

    //Get the physical root and store it
    Global.ApplicationPhysicalPath = Request.PhysicalApplicationPath;

But I can’t get the relative path. I have the following code that works, but it requires it to be placed in an object page:

Global.ApplicationRelativePath = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, "") + Page.ResolveUrl("~/");

thank

+5
source share
2 answers

To get the relative path in application_start, use the following:

 protected void Application_Start(object sender, EventArgs e)
 {
     string path = Server.MapPath("/");
 }
+3
source

In Global.asax, add the following code:

private static string ServerPath { get; set; }

protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        ServerPath = BaseSiteUrl;
    }

    protected static string BaseSiteUrl
    {
        get
        {
            var context = HttpContext.Current;
            if (context.Request.ApplicationPath != null)
            {
                var baseUrl = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/') + '/';
                return baseUrl;
            }
            return string.Empty;
        }
    }
+1
source

All Articles