Cookies不存在(mvc5) [英] Cookies is not persist (mvc5)

查看:62
本文介绍了Cookies不存在(mvc5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



条件中我有一个重叠div。



如果Cookies [ User]为null然后我显示overlay div有两个radio buttong和一个提交按钮。

当用户选择radio选项并单击submit按钮时,当通过ajax调用时,我正在调用一个动作,

哪个设置了cookie。



我在布局页面内推出了Overlay div,所以每次调用都要检查会话。



我的问题是:第一次设置cooikes之后,第二次没有持久。

以下是我设置cookie的方法:



public ActionResult SaveUserTypeCookies(string usertype,string returnUrl)

{

if(Request.Cookies [User]!= null )

{

HttpCookie cookie = Request.Cookies [User];

cookie.Values [UserType] = usertype;

cookie.Expires = DateTime.MaxValue;

Response.SetCookie(cookie);

}

else

{

HttpCookie cookie = new HttpCookie(User);

cookie.Values [UserType] = usertype;

cookie.Expires = DateTime.MaxValue ;

Response.Cookies.Add(cookie);





}

返回Redirect(returnUrl);





以下是我对叠加div的说法:



@if((Request.Cookies [User] == null))

{

Hello All,

I have one overlay div inside a condition.

If Cookies["User"] is null then I show overlay div having two radio buttong and one submit button.
When user select radio option and click submit button,when by ajax call ,I am calling an action ,
Which sets cookies.

I have putted Overlay div inside layout page,so for every call it checkes for session.

My issue is : First time after setting cooikes, it’s not persist for second time.
Below is my method which sets cookies:

public ActionResult SaveUserTypeCookies(string usertype, string returnUrl)
{
if (Request.Cookies["User"] != null)
{
HttpCookie cookie = Request.Cookies["User"];
cookie.Values["UserType"] = usertype;
cookie.Expires = DateTime.MaxValue;
Response.SetCookie(cookie);
}
else
{
HttpCookie cookie = new HttpCookie("User");
cookie.Values["UserType"] = usertype;
cookie.Expires = DateTime.MaxValue;
Response.Cookies.Add(cookie);


}
return Redirect(returnUrl);


Below is my contion for overlay div:

@if ((Request.Cookies["User"]== null))
{





}



}

推荐答案

以下链接将帮助您:

HTTP://www.chwe。 at / 2009/01 / don-t-use-response.cookies-string - to-check-if-a-cookie-exists / [ ^ ]



有时DateTime.MaxValue也会引起麻烦,如下面链接所述:

http://stackoverflow.com/questions/6127123/net-datetime-maxvalue-is-different-once -it-is-stored-in-database [ ^ ]



因此C#和JavaScript可能存在精度问题,所以尝试类似

DateTime.Now.AddHours(12);



并检查它们是否在客户端设置。



如果可以,请尝试这些并分享成功。谢谢。
Below links will help you:
http://www.chwe.at/2009/01/don-t-use-response.cookies-string--to-check-if-a-cookie-exists/[^]

Sometime DateTime.MaxValue can also cause trouble as described in below link:
http://stackoverflow.com/questions/6127123/net-datetime-maxvalue-is-different-once-it-is-stored-in-database[^]

So there may be precision issue with C# and JavaScript, so try something like
DateTime.Now.AddHours(12);

and check whether they are setting at client side or not.

Try those and share if you can get success. Thanks.


感谢您的回复,但它没有帮助,仍有问题:(
Thank you for you response,but it doesn't help,still issue is there :(


这篇关于Cookies不存在(mvc5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆