ASP.NET MVC查询在未请求时部署了Application Server Address

时间:2012-04-13 00:42:02

标签: c# asp.net-mvc url httpcontext

这个问题看起来像another question不是吗?略有差异。我想在初始设置时(在应用程序启动时)知道服务器的部署地址,当时没有请求:

HttpContext.Current.Request

所以我无法查询它以获取当前的URL。还有另一种方式吗?

1 个答案:

答案 0 :(得分:0)

结帐HostingEnvironment课程。如果HostingEnvironment课程中没有您需要的信息,您也可以查看following article。在本文中,作者使用Application_BeginRequest方法而不是Application_Start执行初始化逻辑。它使用锁来确保只执行一次初始化:

void Application_BeginRequest(Object source, EventArgs e)
{

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

    // Attempt to peform first request initialization

    FirstRequestInitialization.Initialize(context);
} 

这里是FirstRequestInitialization类:

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;
        }
    }
}