ServiceStack Cookie未在浏览器中设置

时间:2014-06-20 14:55:10

标签: servicestack servicestack-bsd servicestack-razor

我有一个启用Razor的应用程序和这样的服务:

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

但是,当我尝试在我的Razor View中访问该cookie时,它是空的:

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

当我浏览我的Cookie时,我看不到Terminal

的任何内容

enter image description here

1 个答案:

答案 0 :(得分:4)

我确定你会为这个问题踢自己,但是Response上添加了Cookie,你使用的是Request对象。 :)别担心我也做到了!

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