首次访问后更改主页

时间:2013-09-27 18:58:30

标签: 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 }

        );

    }
}

我猜我需要为Home / FirstTime添加一条路线,但我不知道如何将这些信息存储在网站上。

1 个答案:

答案 0 :(得分:6)

添加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");
        }
    }

您可以控制的Cookie有更多设置,例如到期等。但您现在应该知道方向。