7

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

2 Answers 2

4

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("/");
 }
2

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;
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.