C# 使用asp.net和应用程序状态将站点重定向到移动站点
我有一个asp.net的网站,还有它的手机版。我希望如果用户在移动设备中键入此项: 然后他被指示: 我编写了以下代码:C# 使用asp.net和应用程序状态将站点重定向到移动站点,c#,asp.net,C#,Asp.net,我有一个asp.net的网站,还有它的手机版。我希望如果用户在移动设备中键入此项: 然后他被指示: 我编写了以下代码: protected void Application_AcquireRequestState(object sender, EventArgs e) { HttpContext context = HttpContext.Current; if (context.Request.Browser.IsMobileDevice)
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");
}
}
}
}
}
当用户输入
因此,用户被重定向到:
但当用户类型:
它不会重定向到:
请建议解决方案。问题是字符串。替换() 你能来吗
query = Regex.Replace(query, "id", "name", RegexOptions.IgnoreCase);