Get complete url to a page from global asax
Solution 1
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
Solution 2
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
}
}
Solution 3
It is not possible to get the URL during the Application_Start method of the Global.asax. As @Edwin noted, you can do it inside the Application_BeginRequest, but this fires for every request made, not just the first one. If this is an issue you'll need to setup an initializer that though called for each request, only fires once.
Here's an article on the issue: http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart/
And the relevant bits:
void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpContext context = app.Context;
// Attempt to peform first request initialization
FirstRequestInitialization.Initialize(context);
}
class FirstRequestInitialization
{
private static bool s_InitializedAlready = false;
private static Object s_lock = new Object();
// Initialize only on the first request
public static void Initialize(HttpContext context)
{
if (s_InitializedAlready)
return;
lock (s_lock)
{
if (s_InitializedAlready)
return;
/* << Perform first-request initialization here >> */
s_InitializedAlready = true;
}
}
}
Solution 4
Thanks to Royi I now remembered how to do that (long time I hadn't done it) :
var url = HttpContext.Current.Request.Url;
bar myUrl = url.AbsoluteUri.Replace(url.PathAndQuery, VirtualPathUtility.ToAbsolute("~/WS/someFolder/someService.svc"));
Serge
This place is getting ruined by people that think so high of themselves as sheriffs. Vote to close this profile if you think it's lacking a code sample.
Updated on November 29, 2020Comments
-
Serge over 3 years
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. -
Rohit Arora almost 9 yearsIt gives an error saying "Request is not available in this context".
-
Royi Namir almost 8 years@RohitArora As I said : use
HttpContext.Current.Request
notRequest....
-
Edyn over 7 years@RohitArora You're getting the error because the Request is not available in the Application_Start method.
-
Pierre almost 4 yearsthis returns all requests to all resources, not the URL in the browser