我有一个启用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
:
答案 0 :(得分:4)
我确定你会为这个问题踢自己,但是Response
上添加了Cookie,你使用的是Request
对象。 :)别担心我也做到了!
Response.SetCookie("Terminal", request.TerminalId.ToString());