Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting relative root in Global.Asax

I am trying to store the physical root and relative root of my application in memory at application startup.

I did the physical path like so:

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

But I cannot get the relative path. I have the following code that works, but it requires it to be put in a page object:

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

Thanks

like image 392
TheGateKeeper Avatar asked Jun 06 '12 11:06

TheGateKeeper


Video Answer


2 Answers

In order to get a relative path within application_start use the following:

 protected void Application_Start(object sender, EventArgs e)
 {
     string path = Server.MapPath("/");
 }
like image 130
Dreamwalker Avatar answered Oct 20 '22 17:10

Dreamwalker


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;
        }
    }
like image 35
Dennis Betten Avatar answered Oct 20 '22 16:10

Dennis Betten