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

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


相关文章

DBeaver连接oracle 下载驱动报错

https://blog.csdn.net/weixin_45764765/article/details/124327194...

asp.net core furion 前端传入json,后端api接入并角析

{   "result": {     "cover": {  ...

asp.net core return Content 返回可解析的html

return Content(     "<!DOCTYPE html><html><body&g...

asp.net core SqlSugar对SugarParameter 参数的调试

string sql = "select id,xm from stu where phone=@phone&...

asp.net core指定运行端口 如:http://localhost:5002

网站根目录新建:hosting.json输入:{   "urls": "http://localhost:5002" }...

发表评论    

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