【发布时间】:2012-08-02 10:05:13
【问题描述】:
我有一个带有查看主站点链接的移动站点。
默认情况下,当移动用户访问我的主站点时,它会检测到移动设备并重定向到移动站点。
一旦在移动网站上,如果用户点击“查看主站点”,它会创建一个 cookie 并重定向回主站点。主站点检测到 cookie,因此不会将它们重定向回来;它总是接受主站点作为他们的首选。
问题是,在主站点上,它可以检测到 cookie,但值始终为 null,并且过期时间始终为 DateTime.MinValue。
移动网址 = mobile.mysite.co.uk
主站点 = mysite.co.uk
这是我的代码...
从移动网站链接到主网站
public ActionResult ViewMainSite()
{
string CookieValue = "Always Show the Main Site";
HttpCookie Cookie = new HttpCookie("ShowMainSite");
// Set the cookie value and expiry.
Cookie.Value = CookieValue;
Cookie.Expires = DateTime.Now.AddYears(1);
// Add the cookie.
Response.Cookies.Add(Cookie);
return Redirect("mainSiteURL");
}
主站点操作过滤器 - 检测移动用户
/// <summary>
/// Redirect If Mobile Action filter class
/// </summary>
public class RedirectIfMobile : ActionFilterAttribute
{
/// <summary>
/// override the OnactionExecuting Method
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// check to see if the have the main site cookie
var Cookie = filterContext.HttpContext.Response.Cookies.Get("ShowMainSite");
if (Cookie != null)
{
if (Cookie.Value == null || Cookie.Expires < DateTime.Now)
{
filterContext.HttpContext.Response.Redirect("MobileURL");
}
}
base.OnActionExecuting(filterContext);
}
}
谁能明白为什么会这样?
任何帮助都是最受重视的。
【问题讨论】:
-
我的事情你不能添加 cookie 并在同一个标题上进行重定向。当您想在页面上保存 cookie 时,您不能使用重定向。如果我错了,请告诉我。
标签: c# asp.net-mvc