From the course: Advanced ASP.NET Web API 2.2

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Setting and getting cookies in Web API

Setting and getting cookies in Web API

From the course: Advanced ASP.NET Web API 2.2

Start my 1-month free trial

Setting and getting cookies in Web API

- [Instructor] - Let us now learn how to set and get a cookie in Web API. So let's go to Visual Studio and see it in action. In here we are going to use the Webapi.Di project, so let us set that project as the startup project. Now let us create the new controller. So, Add, Class. We are going to name this controller CookiesController. And for this to be a valid API contoller we need to inherit from the base class ApiController. In here we are going to create an API endpoint which returns an HTTP response message. So for that we write public HttpResponseMessage and then we import the name space System.Net.Http Let us name this method Get. And in here we are going to construct our response. So let's write in here var response is equal to new HttpResponseMessage And now let us create the cookie. So, var cookie is equal to new CookieHeaderValue. Because to add a cookie to an HTTP response, we need to create a cookie…

Contents