我在 asp.net 中有网站,也有它的移动版本。我希望如果用户在移动设备中键入:
http://example.com/ViewVacancy.aspx?ID=8674
然后他被引导到:
http://m.example.com/vacancy.aspx?name=8674
我写了以下代码:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
if (context.Request.Browser.IsMobileDevice)
{
string uri = context.Request.Url.Authority.ToString();
if (context.Request.Url.PathAndQuery.ToLower().Contains("viewvacancy"))
{
string query = context.Request.Url.Query;
query = query.Replace("ID", "Name");
if (uri.ToString().StartsWith("http"))
{
Uri nmobileURi = new Uri(context.Request.Url.Scheme + "://" + "m.example.com/vacancy.aspx" + query);
Response.Redirect(nmobileURi.ToString());
}
}
else
{
Response.Redirect("http://m.example.com");
}
}
if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
{
var userAgent = HttpContext.Current.Request.UserAgent.ToLower();
if (userAgent.Contains("iphone") || userAgent.Contains("ipod"))
{
Response.Redirect("http://example.com/downloadapp.htm");
// iPhone
}
else if (userAgent.Contains("android"))
{
Response.Redirect("http://example.com/downloadgoogleplay.htm");
}
//Create a list of all mobile types
string[] mobiles =
new[]
{ "midp", "j2me", "avant", "docomo",
"novarra", "palmos", "palmsource",
"240×320", "opwv", "chtml",
"pda", "windows ce", "mmp/",
"blackberry", "mib/", "symbian",
"wireless", "nokia", "hand", "mobi",
"phone", "cdm", "up.b", "audio",
"SIE-", "SEC-", "samsung", "HTC",
"mot-", "mitsu", "sagem", "sony"
, "alcatel", "lg", "eric", "vx",
"NEC", "philips", "mmm", "xx",
"panasonic", "sharp", "wap", "sch",
"rover", "pocket", "benq", "java",
"pt", "pg", "vox", "amoi",
"bird", "compal", "kg", "voda",
"sany", "kdd", "dbt", "sendo",
"sgh", "gradi", "jb", "dddi",
"moto", "iphone"
};
//Loop through each item in the list created above
//and check if the header contains that text
foreach (string s in mobiles)
{
if (context.Request.ServerVariables["HTTP_USER_AGENT"].
ToLower().Contains(s.ToLower()))
{
string uri = context.Request.Url.Authority.ToString();
if (context.Request.Url.PathAndQuery.ToLower().Contains("viewvacancy"))
{
string query = context.Request.Url.Query;
query = query.Replace("ID", "Name");
if (uri.ToString().StartsWith("http"))
{
Uri nmobileURi = new Uri(context.Request.Url.Scheme + "://" + "m.example.com/vacancy.aspx" + query);
Response.Redirect(nmobileURi.ToString());
}
}
else
{
Response.Redirect("http://m.example.com");
}
}
}
}
}
当用户键入时它正在工作
所以用户被重定向到:
但是当用户键入时:
http://example.com/viewvacancy.aspx?id=8674
它不会重定向到:
http://m.example.com/vacancy.aspx?name=8674
请提出解决方案。