Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get complete url to a page from global asax

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.

like image 422
Serge Avatar asked Oct 17 '25 07:10

Serge


2 Answers

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 :

enter image description here

  • OriginalString was used cuz you wanted the full origin info.

  • you can also use the one without the port which is AbsoluteURI

like image 54
Royi Namir Avatar answered Oct 19 '25 21:10

Royi Namir


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
    }
}
like image 45
Edwin Mendez Avatar answered Oct 19 '25 21:10

Edwin Mendez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!