【问题标题】:Set and Get the Cookies in ASP.NET web forms在 ASP.NET Web 表单中设置和获取 Cookie
【发布时间】:2015-04-24 08:14:53
【问题描述】:

Asp.net 中的 cookie 快要死了!这是我的代码:

设置cookie:(Upload是一个asp:FileUpload控件,用于上传图片)

   HttpCookie cookie = new HttpCookie("WorkingImage", Upload.FileName.ToString());
  cookie.Expires = DateTime.Now.AddDays(3);
  HttpContext.Current.Response.Cookies.Add(cookie);

这就是我得到它的方式:

 if (HttpContext.Current.Request.Cookies["WorkingImage"] != null && HttpContext.Current.Request.Cookies["WorkingImage"].Value.ToString() != "")
     { //....}

当我运行项目时,cookie 值为 ""。 有什么帮助吗?(请注意,在其他页面中cookie可以设置并正确获取)

【问题讨论】:

    标签: asp.net cookies httpcontext string is-empty


    【解决方案1】:

    如果您在处理程序内使用继承:

     System.Web.SessionState.IRequiresSessionState
    

    像这样:

    public class Handler : IHttpAsyncHandler, System.Web.SessionState.IRequiresSessionState
    

    如需更多 ASP .net 中的 cookie 教程,请访问this。

    【讨论】:

      【解决方案2】:

      这是我们在 cookie 中保存 WorkingImage 名称的示例

              HttpCookie cookie = new HttpCookie("WorkingImage");
              cookie.Value = Upload.FileName.ToString(); // Upload is an asp:FileUpload control name for uploading image
              cookie.Expires = DateTime.Now.AddHours(3);
              Response.SetCookie(cookie);
      

      这是访问我们的 cookie 并检查 cookie 的示例:

              if(Request.Cookies["WorkingImage"] != null)
              {
                  var cookieValue=Request.Cookies["WorkingImage"].Value;
              }
      

      【讨论】:

        猜你喜欢
        • 2014-04-18
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        • 2020-11-16
        • 2014-04-11
        • 2020-05-30
        • 2017-04-15
        • 1970-01-01
        相关资源
        最近更新 更多