【发布时间】:2015-11-30 06:35:27
【问题描述】:
我有一个 ASP.Net MVC Web 应用程序,在这个应用程序中我有 IP 检测工作,IP 检测方法大约需要 30 秒才能获得 IP,这很好。但是我只有 30 秒的时间来运行索引页面以及 IP 检测。意味着如果我调用 IP 检测,那么将没有时间加载索引。我正在从 Application_Start() 调用 IP 检测方法。但是当它运行时主页没有时间加载。 我想在自动加载应用程序后调用IP检测方法。如何可能请帮助。
我的IP检测方法为:
public void GetCityByIP()
{
abcEntities db = new abcEntities();
string IPDetect = string.Empty;
string APIKeyDetect = "Set API key";
string city = "";
string cityName = string.Empty;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
IPDetect = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
IPDetect = "192.206.151.131";
}
string urlDetect = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKeyDetect, IPDetect);
try
{
using (WebClient client = new WebClient())
{
string json = client.DownloadString(urlDetect);
Location location = new JavaScriptSerializer().Deserialize<Location>(json);
List<Location> locations = new List<Location>();
locations.Add(location);
city = location.CityName;
}
}
catch (WebException e)
{
city = "Toronto";
}
var getCityID = db.Macities.Where(c => c.CityName.Contains(city)).ToList();
if ((getCityID != null) && (getCityID.Count > 0))
{
cityName = getCityID.FirstOrDefault().CityName;
}
else
{
getCityID = db.Macities.Where(c => c.CityName.Contains("Toronto")).ToList();
cityName = getCityID.FirstOrDefault().CityName;
}
HttpContext.Current.Response.Cookies["CityName"].Value = cityName;
}
我想设置 cookie 并将其用作整个应用程序中检测到的 IP 城市。我从 Start 方法中将其调用为:
protected void Application_Start()
{
GetCityByIP();
}
IP 检测方法也在全局文件中。我在数据库中有有限的城市,所以这就是为什么我使用数据库并匹配数据库城市中的城市(如果存在)然后 IP 方法设置在 cookie 中检测到的城市,其他明智的默认城市将在 cookie 中设置。 提前致谢。
【问题讨论】:
-
您需要每个应用程序一次还是每个访问者一次?
-
那么你有一个不同的问题。弄清楚为什么这需要 30 秒。你不能让用户等待。
标签: c# asp.net asp.net-mvc