私は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
それに対する解決策を提案してください。