I have a ASP.Net MVC Web application, In this application i have work to IP detection, IP detection method is taking near about 30 seconds to get the IP, That is fine. But I have only 30 second to run the index page as well as IP detection. Means if i am calling IP detection then there will be no time to load index. I am calling IP detection method from Application_Start(). but when it runs main page have no time to load. I want to call IP detection method after load the application automatically.How it is possible please help.
I have IP detection method as:
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;
}
I want to set cookies and use it as Detected IP city in whole application.I am calling it from Start method as :
protected void Application_Start()
{
GetCityByIP();
}
IP detection method is also in global file. I have Limited cities in database so that's why i am using database and match city in database cities if it exist then IP method set the city detected in cookies other wise default city will be set in cookies. Thanks in advance.