【发布时间】:2012-08-20 07:57:09
【问题描述】:
我正在尝试使用 httpRequestBegin 管道处理器执行此操作,但我似乎无法从给定的 HttpRequestArgs 参数访问用户的 IP 地址。
当我实现一个有这个方法的类时
public void Process(HttpRequestArgs args) {
string ipAddress = args.Context.Request.UserHostAddress; // Not working
string state = GetState(ipAddress); // already implemented elsewhere
RedirectUserByState(state); // already implemented elsewhere
}
我认为这可能包含用户的 IP 地址
args.Context.Request.UserHostAddress
但它反而会导致此错误(堆栈跟踪表明它源自 Process 方法):
System.Web.HttpException: Request is not available in this context
有什么想法吗?谢谢!
编辑,这是在 Sitecore 6.1 和 web.config 中的
<pipelines>
<!--...-->
<httpRequestBegin>
<!--...-->
<processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel"/>
<processor type="MySolution.Redirector, MySolution"/>
<processor type="Sitecore.Pipelines.HttpRequest.LayoutResolver, Sitecore.Kernel"/>
<!--...-->
</httpRequestBegin>
<!--...-->
</pipelines>
此服务器可能位于负载平衡器后面。
编辑 2: 看起来我 正在 尝试在 Process() 函数中访问这两个,这就是导致错误的原因:
Sitecore.Context.Request
Sitecore.Context.Response
【问题讨论】:
-
这应该可以正常工作(至少它对我有用)。你能告诉我们你在配置中定义管道的位置吗?另外,您是否支持负载均衡器?
-
另外,您可能希望确保
GetState()和RedirectUserByState()不使用 HttpContext.Current.Request/Response。如果他们这样做,您需要将 args.Context 传递给这些方法。 -
感谢您的回复。这些方法都没有使用 HttpContext.Current
标签: c# geolocation ip sitecore pipeline