我需要在所有操作中使用服务器的 IP。
当我尝试将其放入控制器构造函数中时,它会引发错误:
_runningServer = AppConstants.Common.ServerDetect[Request.ServerVariables["LOCAL_ADDR"].Substring(0, 4)];
我发现的原因是尚未创建 http 上下文。
我尝试使用System.Web.HttpContext.Current
,但它没有解决问题。
我在 Intranet 应用程序中使用服务器 IP 作为应用程序以各种方式配置自身的自动方式。
更新:
似乎覆盖 Intialize() 对我的情况来说是一个更好的解决方案:
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
_runningServer =AppConstants.Common.ServerDetect[System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"].Substring(0, 4)];
}