2

This question looks like another question doesn't it? Slight difference. I want to know the deployed address of the server while I am doing initial setup (in Application start) and at that time there is no request:

HttpContext.Current.Request

So I cannot query it to get the current URL. Is there another way?

2
  • 1
    Two things that I'm curious of...what do you need this for (might be a better way?) and what version of the mvc framework are you using? Commented Apr 13, 2012 at 2:15
  • I'm doing a dodgy hack for development where I am locating wcf services in code and know the relative path to the service, from the current url. MVC 3 Commented Apr 13, 2012 at 4:25

1 Answer 1

0

Checkout the HostingEnvironment class. You might also take a look at the following article if the information you need is not present in the HostingEnvironment class. In this article the author performs the initialization logic in the Application_BeginRequest method instead of Application_Start. It uses a lock to ensure that this initialization is performed only once:

void Application_BeginRequest(Object source, EventArgs e)
{

    HttpApplication app = (HttpApplication)source;
    HttpContext context = app.Context;

    // Attempt to peform first request initialization

    FirstRequestInitialization.Initialize(context);
} 

and here's the FirstRequestInitialization class:

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.