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
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("/");
}
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With