asp.net core razor 写入读取cookie集合

浪淘沙4周前后端35
@{
    Layout = null;
}

<form method="post">
    <button type="submit"  asp-page-handler="save">写入cookie</button>
    <button type="submit" asp-page-handler="read">读取cookie</button>
    <button type="submit" asp-page-handler="del">删除cookie</button>
</form>

@if (Model.msg != null)
{
    <p>@Model.msg</p>
}
 [BindProperty]
 public string msg{ get; set; }
 public void OnGet()
 {
 }

 public IActionResult OnPostSave()
 {
     //写入cookie
     Dictionary<string, string> dict = new Dictionary<string, string>();
     dict.Add("id", "1");
     dict.Add("xm", "lgx");
     dict.Add("groupid", "2");
     dict.Add("sign", "123445");

     var option = new CookieOptions();
     option.Expires = DateTime.Now.AddDays(1);
     App.HttpContext.Response.Cookies.Append("myinfo", JsonSerializer.Serialize(dict), option);
     msg = "写入成功";
     return Page();

    
 }
 public IActionResult OnPostRead()
 {
     //读取cookie
     var myinfo = App.HttpContext.Request.Cookies["myinfo"];

     if (myinfo==null)
     {
         msg = "不存在cookie";
         return Page();
     }

     var dict = JsonSerializer.Deserialize<Dictionary<string, string>>(myinfo);
     string id = dict["id"];
     string xm = dict["xm"];
     string groupid = dict["groupid"];
     string sign = dict["sign"];
     msg = "读取成功"+ myinfo + "--" + id + "--" + xm + "--" + groupid + "--" + sign;
     return Page();
 }

 public IActionResult OnPostDel()
 {
     //删除cookie
     App.HttpContext.Response.Cookies.Delete("myinfo");

     msg = "删除成功";
     return Page();
 }


相关文章

asp.net core ZipDeploy iis发布 忽略某一件文件夹被删除

  services.AddZipDeploy(o => o.IgnorePathStarting("wwwroot/")); //iis发布...

asp.net core Aspose.Words 文本换行

using Aspose.Words;// 替换换行符为 Word 中的换行符var placeholders = new&nbs...

asp.net core razor onpost

不管 OnGet 填充多少东西OnPost 必须再填一次因为 Razor Pages 不会“记住” ViewModelPOST 后是重新执行一个请求,Model 全部重新创建。不像 WebForms,...

asp.net core 绑定下拉

   public List<SelectListItem> zplx { get; set; }  [BindProperty]  public...

点击按钮,禁用提示,加导出word

@page @model xxtsoft.Web.Entry.Pages.sysadmins.kh.kh_outModel @{     Lay...

_LayoutAdmin.cshtml 取得url,调用ViewComponent 视图

_LayoutAdmin.cshtml 取得url,调用ViewComponent 视图

<body id="watermark-parent"> @{ var name = Furion.App.HttpC...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。