コード例 #1
0
        public async Task <IActionResult> ContentsView(int seq, int p) //View()는 메소드로 쓸수 없음 route 로 처리
        {
            ViewData["page"] = p;
            var user  = User.GetLoginInfo();
            var model = await BoardModel.GetAsync("GENERAL", seq);

            if (!(model.REG_USER == user?.USER_ID))
            {
                //HttpContext.Connection.Id
                //동일 세션의 중복 ViewCount를 막기위해 쿠키를 써봄
                var cookieId = "BOARD_GENERAL";

                if (Request.Cookies.ContainsKey(cookieId) == false)
                {
                    Response.Cookies.Append(cookieId, $"[{seq}]", new Microsoft.AspNetCore.Http.CookieOptions()
                    {
                        HttpOnly = true
                    });
                    model.AddViewCount();
                }
                else
                {
                    var list = JsonConvert.DeserializeObject <List <int> >(Request.Cookies[cookieId]);
                    if (list.Any(x => x == seq) == false)
                    {
                        model.AddViewCount();

                        list.Add(seq);
                        Response.Cookies.Append(cookieId, JsonConvert.SerializeObject(list), new Microsoft.AspNetCore.Http.CookieOptions()
                        {
                            HttpOnly = true
                        });
                    }
                }
            }

            return(View(model));
        }