servicestackservicestack-bsdservicestack-razor

ServiceStack Cookie not setting in browser


I have an application with Razor Enabled and a service like this:

public object Post(SelectTerminalRequest request)
{
    var location = base.Request.AbsoluteUri.CombineWith(request.TerminalId, "/flights");
    if (Request.Cookies.ContainsKey("Terminal"))
    {
        Request.Cookies.Remove("Terminal");
    }
    Request.Cookies.Add("Terminal",
        new Cookie("Terminal", request.TerminalId.ToString()) 
            { Expires = DateTime.Now.AddYears(1) });
    return new HttpResult(HttpStatusCode.Redirect)
    {
        Location = location
    };
}

However, when I try and access that cookie in my Razor View, its empty:

@{
     Cookie cookie;
     Request.Cookies.TryGetValue("Terminal", out cookie);
     var baseUri = Request.GetApplicationUrl() + "/terminals";
     var redirectUrl = cookie != null ? baseUri + "/" + cookie.Value + "/flights" : baseUri;
 }

When I browse my cookies, I don't see anything with Terminal:

enter image description here


Solution

  • You'll kick yourself for this one, I am sure, but adding cookies should be on the Response, you used the Request object. :) Don't worry I've done that too!

    Response.SetCookie("Terminal", request.TerminalId.ToString());