在我的api控制器中,我访问cookie值:
public class SampleController : ApiController
{
protected int? GetUserFromCookie()
{
if (HttpContext.Current.Request.Cookies["User"] != null)
{
//do something
}
}
}
我为此创建单元测试并尝试执行以下操作:
var httpRequest = new HttpRequest("", "http://stackoverflow/", "");
var stringWriter = new System.IO.StringWriter();
var httpResponce = new HttpResponse(stringWriter);
var httpContext = new HttpContext(httpRequest, httpResponce);
HttpContext.Current = httpContext;
但是HttpContext.Current
仍然是空的。
有什么想法吗?