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
它在我的本地机器上运行得很好,但是当它被发布到IIS7时,有一个例外的说法
System.Web.HttpException: Request is not available in this context
Anyone know how to achieve this?
有人知道怎么做到吗?
3 个解决方案
#1
59
When your web application starts, there is no HTTP request being handled.
当web应用程序启动时,没有处理HTTP请求。
You may want to handle define the Application_BeginRequest(Object Sender, EventArgs e) method in which the the Request context is available.
您可能想要处理定义Application_BeginRequest(对象发送方、EventArgs e)方法,其中请求上下文可用。
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;
}
}
#2
9
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.
接受的答案是好的,但是在大多数情况下(如果第一个请求是HTTP请求),您最好使用Session_Start事件,每个用户每20分钟调用一次(不确定会话的有效时间)。每个请求都会触发Application_BeginRequest。
public void Session_Start(Object source, EventArgs e)
{
//Request / Request.Url is available here :)
}
#3
1
Just answering this so if someone ever decides to actually search on this topic...
只要回答这个问题,如果有人决定真的搜索这个话题……
This works on application start in any mode...
这适用于任何模式下的应用程序启动……
typeof(HttpContext).GetField("_request", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(HttpContext.Current)
#1
59
When your web application starts, there is no HTTP request being handled.
当web应用程序启动时,没有处理HTTP请求。
You may want to handle define the Application_BeginRequest(Object Sender, EventArgs e) method in which the the Request context is available.
您可能想要处理定义Application_BeginRequest(对象发送方、EventArgs e)方法,其中请求上下文可用。
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;
}
}
#2
9
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.
接受的答案是好的,但是在大多数情况下(如果第一个请求是HTTP请求),您最好使用Session_Start事件,每个用户每20分钟调用一次(不确定会话的有效时间)。每个请求都会触发Application_BeginRequest。
public void Session_Start(Object source, EventArgs e)
{
//Request / Request.Url is available here :)
}
#3
1
Just answering this so if someone ever decides to actually search on this topic...
只要回答这个问题,如果有人决定真的搜索这个话题……
This works on application start in any mode...
这适用于任何模式下的应用程序启动……
typeof(HttpContext).GetField("_request", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(HttpContext.Current)