我有一个小型的移动重定向项目,可以在我的本地计算机上完美运行,但在生产中,它会在搜索点击时中断。
以下是一些更多信息: - site.master.cs中Page_Load中的重定向子例程 - 我使用if / else语句来遍历所有移动URL参数 - 在我的本地机器上完美运行 - 我没有合并任何会话变量(删除它们),这是先前破坏它。
以下是代码:
if (Request.Browser.IsMobileDevice == true){
HttpRequest httpRequest = HttpContext.Current.Request;//GET current http request header
string mRoot = "m.website.com";//Mobile root URL
string path = httpRequest.Url.AbsoluteUri.ToString().ToLower();//GET original destination path
string protocol = "http://";//protocol used
string RedirectTo = protocol + mRoot;//Mobile Destination URL
string webRoot = protocol + "www.website.com/autos.aspx";// Root URL
string userAgent = Request.UserAgent.ToString().ToLower();
string QueryString = Request.QueryString.ToString().ToLower();
string listingID = Request.PathInfo.ToString();
string make = string.Empty;
string model = string.Empty;
string zip = GetUserZip();//Get Zipcode
string distance = Request.Cookies["Search"]["distance"];
string redirectedUrl = string.Empty;//Destination URL
string status = string.Empty;
string location = string.Empty;
CommonService _common = new CommonService();
Uri uri = new Uri(_common.GetSiteBaseURL());
NameValueCollection query = HttpUtility.ParseQueryString(uri.Query);
// www.website.com/autos/results/all/all/refine?pagenum=1&perpage=30&sort=price desc&zipcode=33716&inv=false
//
// "Default"
// If “/results/” is NOT contained in the URL and “/listing/” is NOT contained in the URL
if ((!path.Contains("/results/", StringComparison.OrdinalIgnoreCase)) && (!path.Contains("/listing/", StringComparison.OrdinalIgnoreCase)))
{
if ((!path.Contains(make)) && (!path.Contains(model)))
{
// Send to Default page on Mobile
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", "http://m.website.com/autos.aspx");
Response.Redirect(RedirectTo + "/autos.aspx");
}
}
// "VDP"
// If “/results/” is NOT contained in the URL and “/listing/” IS contained in the URL Send to the VDP page on Mobile
if ((!path.Contains("/results/", StringComparison.OrdinalIgnoreCase)) && (path.Contains("/listing/", StringComparison.OrdinalIgnoreCase)))
{
if (path.Contains("/autos/", StringComparison.OrdinalIgnoreCase))
{
string x = path.ToString(); // GET itemid here
string y = x.Split('/')[8];
string itemid = y;
// Capture everything after /listing/
if (itemid != null)//Add more verification to itemid
{
// Send to VDP mobile page
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", "http://m.website.com/listingdetail.aspx");
Response.Redirect(protocol + mRoot + "/listingdetail.aspx" + "?listID=" + itemid);
}
}
string p = path.ToString(); // GET itemid here
string j = p.Split('/')[5];
string _itemid = j;
// Send to VDP mobile page
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", "http://m.website.com/listingdetail.aspx");
Response.Redirect(protocol + mRoot + "/listingdetail.aspx" + "?listID=" + _itemid);
}
// "Search Results"
// If “/results/” IS contained in the URL, Send to the SearchResults page on Mobi
if (path.Contains("/results/", StringComparison.OrdinalIgnoreCase))
{
if (path.Contains("/autos/"))
{
string g = path.ToString();
make = g.Split('/')[5];
string m = make.ToString();
model = g.Split('/')[6];
string mm = model.ToString();
// If there are no more characters in the URL after MAKE
if ((path.Contains(m, StringComparison.OrdinalIgnoreCase)) && (path.Contains("&zipcode=", StringComparison.OrdinalIgnoreCase)))// && (path.Contains("&model=", StringComparison.OrdinalIgnoreCase)))
{
if (mm.Contains("--", StringComparison.OrdinalIgnoreCase))
{
// Trim one of the -'s from model if it has two '--'
mm = (mm.Trim().Replace("--", "-"));
}
// END if (path.Contains("--", StringComparison.OrdinalIgnoreCase))
Response.Status = "301 Moved Permanently";
Response.Redirect(protocol + mRoot + "/listings.aspx?sort=HighestPrice&make=" + m + "&model=" + mm + "&zip=" + zip + "&miles=" + distance);
}
if (path.Contains("&zipcode=", StringComparison.OrdinalIgnoreCase))
{
Response.Status = "301 Moved Permanently";
Response.Redirect(protocol + mRoot + "/listings.aspx?sort=HighestPrice" + "&make=" + m + "&zip=" + zip + "&miles=" + distance);
}
// Set URL to URL + MAKE
if (mm.Contains("all"))
{
Response.Status = "301 Moved Permanently";
Response.Redirect(protocol + mRoot + "/listings.aspx?sort=HighestPrice" + "&make=" + m + "&zip=" + zip + "&miles=" + distance);
}
// Otherwise set URL to URL + Make + Model
Response.Status = "301 Moved Permanently";
Response.Redirect(protocol + mRoot + "/listings.aspx?sort=HighestPrice" + "&make=" + m + "&model=" + mm);
if (path.Contains("&zipcode=", StringComparison.OrdinalIgnoreCase))
{
Response.Status = "301 Moved Permanently";
Response.Redirect(protocol + mRoot + "/listings.aspx?sort=HighestPrice" + "&make=" + m + "&zip=" + zip);
}// END if (path.Contains("&zip=", StringComparison.OrdinalIgnoreCase))
// Send to search results on Mobi
// Set _make = text immediately following '/results/' and ending at next '/'
// make = all
if (path.Contains("/all/", StringComparison.OrdinalIgnoreCase))
{
// Set _model = text immediately following '/results/_make/' and ending at next '/'
Response.Status = "301 Moved Permanently";
Response.Redirect(protocol + mRoot + "/listings.aspx?sort=HighestPrice" + mm + "/all/" + distance);
} // END if (path.Contains("/all/", StringComparison.OrdinalIgnoreCase))
// If MODEL = “all/” then
if (path.Contains("/all/all/", StringComparison.OrdinalIgnoreCase))
{
//
Response.Status = "301 Moved Permanently";
Response.Redirect(protocol + mRoot + "/listings.aspx?sort=HighestPrice" + "/all/all/" + distance);
} // END if (path.Contains("/all/all/", StringComparison.OrdinalIgnoreCase))
// If MODEL contains “--" then make sure in redirect that it only gets 1 '-'
if (mm.Contains("--", StringComparison.OrdinalIgnoreCase))
{
mm = (mm.Trim().Replace("‑", "-"));
// Set ZIP = Text immediately following “&zipcode =” and ending at next “&”
if (path.Contains("&zipcode=", StringComparison.OrdinalIgnoreCase))
{
Response.Status = "301 Moved Permanently";
Response.Redirect(protocol + mRoot + "/listings.aspx?sort=HighestPrice" + "?make=" + m + "&model=" + mm + "&zip=" + zip);
} // END if (path.Contains("&zip=", StringComparison.OrdinalIgnoreCase))
if (!path.Contains("&miles", StringComparison.OrdinalIgnoreCase))
{
distance = "1500";
// Set URL to URL + “&miles=1500” - This is the value used for unlimited distance
Response.Status = "301 Moved Permanently";
Response.RedirectPermanent(protocol + mRoot + "/listings.aspx?sort=HighestPrice" + "&make=" + m + "&miles=" + distance);
} // END if (!path.Contains("&miles", StringComparison.OrdinalIgnoreCase))
} // END if (path.Contains("/results/"))
// All other links will go here...
// Call the Redirect() method
// Redirects to new URL specifying URL
Response.RedirectPermanent(protocol + mRoot + "/autos.apsx");
}
string r = Request.FilePath.ToString();
string Make = r.Split('/')[2];
string _m = Make.ToString();
string Model = r.Split('/')[3];
string _mm = model.ToString();
// If there are no more characters in the URL after MAKE
if ((path.Contains(_m, StringComparison.OrdinalIgnoreCase)) && (path.Contains("&zipcode=", StringComparison.OrdinalIgnoreCase)))// && (path.Contains("&model=", StringComparison.OrdinalIgnoreCase)))
{
if (_mm.Contains("--", StringComparison.OrdinalIgnoreCase))
{
// Trim one of the -'s from model if it has two '--'
_mm = (_mm.Trim().Replace("--", "-"));
}
// END if (path.Contains("--", StringComparison.OrdinalIgnoreCase))
Response.Status = "301 Moved Permanently";
Response.Redirect(protocol + mRoot + "/listings.aspx?sort=HighestPrice&make=" + _m + "&model=" + _mm + "&zip=" + zip + "&miles=" + distance);
}
}
}
}
如果我没有提供足够的信息,请告诉我。我已经在这方面工作了几天,需要让它工作。我还需要知道母版页是否是最佳位置,或者我应该创建一个新的.cs文件,并从需要重定向到移动设备的每个页面调用该方法。
答案 0 :(得分:0)
找到解决方案。谢谢大家的评论。我删除了错误的代码行(string location = string.Empty)。我发现我不需要获取此代码块中的位置,以便重定向正常工作。