Id' like to retrieve a url to my website from global asax. This url has to be complete (protocol, domain, etc.). Is there an easy way to do that?
I tried VirtualPathUtility.ToAbsolute
but it only gives a relative path.
Try this :
HttpContext.Current.Request.Url.OriginalString
This way you can access the url from global asax.
P.s. you could have done it yourself by debugging :
OriginalString
was used cuz you wanted the full origin info.
you can also use the one without the port which is AbsoluteURI
You can achieve this by using Application_BeginRequest method in your Global.asax and HttpApplication.Context.Request.Url. Keep in mind that the method is going to fire for every request.
public class Global : System.Web.HttpApplication
{
private void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_BeginRequest(Object source, EventArgs e)
{
var app = (HttpApplication)source;
var uriObject = app.Context.Request.Url;
//app.Context.Request.Url.OriginalString
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs on application error
}
private void RegisterRoutes(RouteCollection routes)
{
// Code that runs on register routes
}
}
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