In this article I will explain with an example, how to get (read) Cookie value inside Controller in ASP.Net MVC Razor.
When the Read Cookie Button is clicked, the Cookie value will be read inside the Controller in ASP.Net MVC Razor.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 
Controller
The Controller consists of the following three Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for writing Cookie
When the Write Cookie Button is clicked, WriteCookie Action method is executed which saves the value of the Name TextBox to the Browser Cookie using the object of the HttpCookie class.
Finally, the Cookie is added to the Response.Cookies collection.
 
Action method for reading Cookie
When the Read Cookie Button is clicked, ReadCookie Action method is executed which fetches the HttpCookie object from the Request.Cookies collection using its Key.
The value read from the Cookie is set in TempData object, which is later displayed using JavaScript Alert Message Box.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult WriteCookie()
    {
        //Create a Cookie with a suitable Key.
        HttpCookie nameCookie = new HttpCookie("Name");
 
        //Set the Cookie value.
        nameCookie.Values["Name"] = Request.Form["name"];
 
        //Set the Expiry date.
        nameCookie.Expires = DateTime.Now.AddDays(30);
 
        //Add the Cookie to Browser.
        Response.Cookies.Add(nameCookie);
 
        return RedirectToAction("Index");
    }
 
    [HttpPost]
    public ActionResult ReadCookie()
    {
        //Fetch the Cookie using its Key.
        HttpCookie nameCookie = Request.Cookies["Name"];
 
        //If Cookie exists fetch its value.
        string name = nameCookie != null ? nameCookie.Value.Split('=')[1] : "undefined";
 
        TempData["Message"] = name;
 
        return RedirectToAction("Index");
    }
}
 
 
View
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case is it set to NULL as the Action method name is set directly on the Submit Button using FormAction attribute.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consists of a TextBox and two Submit Buttons i.e. for Reading Cookie and Writing Cookie.
Each Submit Button has been set with a FormAction attribute which specifies the Action method which will be called when the Submit Button is clicked.
When the Read Cookie button is clicked, the value from the TempData object is displayed using JavaScript Alert Message Box.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm(null, "Home", FormMethod.Post))
    {
        <span>Name: </span><input type="text" id="txtName" name="name"/><br/><br/>
        <input type="submit" id="btnWrite" formaction="@Url.Action("WriteCookie")" value="Write Cookie"/>
        <input type="submit" id="btnRead" formaction="@Url.Action("ReadCookie")" value="Read Cookie"/>
    }
    @if (TempData["Message"] != null)
    {
        <script type="text/javascript">
            window.onload = function () {
                alert('@TempData["Message"]');
            };
        </script>
    }
</body>
</html>
 
 
Screenshot
Get (Read) Cookie value inside Controller in ASP.Net MVC
 
 
Downloads