1

我有一个启用 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 视图中访问该 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

在此处输入图像描述

4

1 回答 1

4

我敢肯定,您会为此而自责,但是添加 cookie 应该在Response您使用的Request对象上。:) 别担心,我也这样做了!

Response.SetCookie("Terminal", request.TerminalId.ToString());
于 2014-06-20T15:03:07.970 回答