Organizational Research By

Surprising Reserch Topic

how to get full host name port number in application start of global aspx


how to get full host name port number in application start of global aspx  using -'c#,asp.net,url,request,hostname'

I tried

Uri uri = HttpContext.Current.Request.Url;
String host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;


and it worked well on my local machine, but when being published to IIS7, there is an exception saying

System.Web.HttpException: Request is not available in this context


Anyone know how to achieve this?
    

asked Sep 9, 2015 by Mari21Enfyjz
0 votes
31 views



Related Hot Questions

4 Answers

0 votes

When your web application starts, there is no HTTP request being handled.

You may want to handle define the Application_BeginRequest(Object Sender, EventArgs e) method in which the the Request context is available.

Edit: Here is a code sample inspired by the Mike Volodarsky's blog that Michael Shimmins linked to:

    void Application_BeginRequest(Object source, EventArgs e)
    {
        HttpApplication app = (HttpApplication)source;
        var host = FirstRequestInitialisation.Initialise(app.Context);
    }

    static class FirstRequestInitialisation
    {
        private static string host = null;
        private static Object s_lock = new Object();

        // Initialise only on the first request
        public static string Initialise(HttpContext context)
        {
            if (string.IsNullOrEmpty(host))
            {
                lock (s_lock)
                {
                    if (string.IsNullOrEmpty(host))
                    {
                        var uri = context.Request.Url;
                        host = uri.GetLeftPart(UriPartial.Authority);
                    }
                }
            }

            return host;
        }
    }
answered Sep 9, 2015 by JuliusMackni
0 votes

Sounds like you're running in integrated mode, not classic (which I think you're running on your local).

The problem is that Request isn't available in Application_Start in IIS7.

Have a look at:

http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx

Which explains it nicely.

answered Sep 9, 2015 by TeganURKXmuo
0 votes

The accepted answer is good, but in most cases (if the first request is a HTTP Request) you should better use the Session_Start event, which is called once per user every 20 minutes or so (not sure how long the session is valid). Application_BeginRequest will be fired at every Request.

public void Session_Start(Object source, EventArgs e)
{
   //Request / Request.Url is available here :)
}
answered Sep 9, 2015 by FYWJessievyt
0 votes

Just answering this so if someone ever decides to actually search on this topic...

This works on application start in any mode...

typeof(HttpRequest).GetField("_request", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(HttpContext.Current)
answered Sep 9, 2015 by SteNixon

...