我有一个名为SurveyCookie的cookie。像这样创建:
var cookie = new HttpCookie("SurveyCookie");
cookie.Values["surveyPage"] = "1";
cookie.Values["surveyId"] = "1";
cookie.Values["surveyTitle"] = "Definietly not an NSA Survey....";
cookie.Values["lastVisit"] = DateTime.UtcNow.ToString();
cookie.Expires = DateTime.UtcNow.AddDays(30);
Response.Cookies.Add(cookie);
哪个很棒。现在,当我想像这样更改值“ surveyPage”时,问题就来了。
下面将创建一个新的cookie,这不是我想要的。
int cookieValue = Convert.ToInt32(Request.Cookies["SurveyCookie"]["surveyPage"]) + 1;
Response.Cookies["SurveyCookie"]["surveyPage"] = cookieValue.ToString();
然后我尝试了下面的这段代码,它也不起作用。 SurveyPage在应为2时仍为1。
Request.Cookies["SurveyCookie"]["surveyPage"] = cookieValue.ToString();
由于上述两种方法均无效,因此如何更改SurveyPage的cookie值?
参考方案
从ASP.NET Cookies Overview:
您不能直接修改Cookie。相反,更改Cookie
包括创建具有新值的新Cookie,然后发送
Cookie到浏览器以覆盖客户端上的旧版本。
您可以尝试以下方法:
HttpCookie cookie = Request.Cookies["SurveyCookie"];
if (cookie == null)
{
// no cookie found, create it
cookie = new HttpCookie("SurveyCookie");
cookie.Values["surveyPage"] = "1";
cookie.Values["surveyId"] = "1";
cookie.Values["surveyTitle"] = "Definietly not an NSA Survey....";
cookie.Values["lastVisit"] = DateTime.UtcNow.ToString();
}
else
{
// update the cookie values
int newSurveyPage = int.Parse(cookie.Values["surveyPage"]) + 1;
cookie.Values["surveyPage"] = newSurveyPage.ToString();
}
// update the expiration timestamp
cookie.Expires = DateTime.UtcNow.AddDays(30);
// overwrite the cookie
Response.Cookies.Add(cookie);
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
java.net.URI.create异常 - javajava.net.URI.create("http://adserver.adtech.de/adlink|3.0") 抛出java.net.URISyntaxException: Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0 虽然n…
Mongo汇总 - javascript我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …
如何将POCO类传递给.NET Core配置 - c#我正在进行.NET Core应用程序的集成测试,并且想要使用一些测试配置。我的配置是一个POCO类,该类通过appsettings.json配置,然后通过IOptions<T>使用。在我的测试中,我想使用该类的实例。这是代码: var mySettings = GetTestSettings(); // factory method return…
如何在Wiremock中为JUNIT匹配精确的json - java我正在使用Wiremock在Spring启动应用程序中模拟Junit的REST服务。我的问题是,我无法匹配多个匹配模式。 Junit.javaStringValuePattern pattern = WireMock.matching(".*"); givenThat(post(urlEqualTo("/softwares…