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

浪淘沙4个月前后端87
@{
    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();
 }


相关文章

json解析

{   "accessToken": "xxxxxx",   "refreshToken&quo...

Razor 遍历 DataTable

@using System.Data <table class="table table-bordered table-striped&n...

asp.net core furion swagger生产环境中 隐藏

asp.net core furion swagger生产环境中 隐藏

开发环境中显示:生产环境中隐藏:...

asp.net core url编码

using System.Text.Encodings.Web; string url = "测试 参数 &&nbs...

asp.net razor post

asp.net razor post

@page @model xxtsoft.Web.Entry.Pages.qd.IndexModel @{     Layout =&...

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

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

发表评论    

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