这个问题看起来像another question不是吗?略有差异。我想在初始设置时(在应用程序启动时)知道服务器的部署地址,当时没有请求:
HttpContext.Current.Request
所以我无法查询它以获取当前的URL。还有另一种方式吗?
答案 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;
}
}
}