dan*_*tch 3 asp.net asp.net-mvc-4
现在我的项目默认为Home/Index作为我的网站开始页面.我想这样做,所以当有人第一次来到网站时,他们会去Home/FirstTime以及随后的所有时间他们返回网站他们会回到主页/索引
我在App_Start文件夹中的RouteConfig.cs中有此代码
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
我猜我需要为Home/FirstTime添加一条路线,但我不知道如何将这些信息存储在他们已经到过的网站上.
添加Cookie以确定用户是否是第一次访问者.
public ActionResult Index()
{
string cookieName = "NotFirstTime";
if(this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains(cookieName ))
{
// not first time
return View();
}
else
{
// first time
// add a cookie.
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value = "anything you like: date etc.";
this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
// redirect to the page for first time visit.
return View("FirstTime");
}
}
Run Code Online (Sandbox Code Playgroud)
您可以控制的Cookie有更多设置,例如到期等.但您现在应该知道方向.
| 归档时间: |
|
| 查看次数: |
958 次 |
| 最近记录: |