Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change home page after first visit

Right now my project defaults to Home/Index as my sites start page. I want to make it so the first time someone comes to site they go to Home/FirstTime and all subsequent times they return to site they go to Home/Index

I have this code in RouteConfig.cs in App_Start folder

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 }

        );

    }
}

I am guessing I need to add a route for Home/FirstTime, but I dont know how to store the info on weather they have been to site previsouly.

like image 461
dan_vitch Avatar asked Dec 02 '25 21:12

dan_vitch


1 Answers

Add a cookie to identify if the user is the first time visitor.

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

There are more settings for a cookie that you can control, like expiration etc. But you should know the direction now.

like image 56
Blaise Avatar answered Dec 05 '25 13:12

Blaise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!