public async Task <ActionResult <AccessHistoryLog> > PostAccessHistoryLog(AccessHistoryLog accessHistoryLog) { _context.AccessHistoryLogs.Add(accessHistoryLog); await _context.SaveChangesAsync(); return(CreatedAtAction("GetAccessHistoryLog", new { id = accessHistoryLog.Id }, accessHistoryLog)); }
public async Task <IActionResult> PutAccessHistoryLog(int id, AccessHistoryLog accessHistoryLog) { if (id != accessHistoryLog.Id) { return(BadRequest()); } _context.Entry(accessHistoryLog).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AccessHistoryLogExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ResultDto> CreateAccessHistoryLog(AccessHistoryLog accessHistoryLog) { await _context.AccessHistoryLog.AddAsync(accessHistoryLog); return(new ResultDto() { Success = await _context.SaveChangesAsync() > 0 ? true : false }); }
/// <summary> /// entity class不使用依赖注入 /// </summary> /// <param name="httpContext"></param> /// <returns>返回string类型的字符串,格式为"message:result"</returns> public string AddAccessHistoryLogOfEntity(HttpContext httpContext, string sessionId) { string Path = httpContext.Request.Path; _accessHistoryLog = new AccessHistoryLog(); _accessHistoryLog.IpAddress = httpContext.Connection.RemoteIpAddress.ToString(); _accessHistoryLog.DateTime = DateTime.Now; _accessHistoryLog.AccessPath = Path; _accessHistoryLog.SessionId = sessionId; return(AddAccessHistoryLog(_accessHistoryLog)); }
public string AddAccessHistoryLog(AccessHistoryLog newAccessHistoryLog) { string message = "添加成功"; bool result = true; try { _context.AccessHistoryLogs.Add(newAccessHistoryLog); _context.SaveChanges(); } catch { message = "添加失败"; result = false; return($"{message}:{result}"); } return($"{message}:{result}"); }
/// <summary> /// entity class不使用依赖注入 /// </summary> /// <param name="httpContext"></param> /// <returns>成功时返回新增的记录</returns> public AccessHistoryLog AddAccessHistoryLog1(HttpContext httpContext) { ///读取Cookie,根据Cookie来判断是否计入有效点击(追踪其记录访问) ///判断Cooike是否存在于redis中,存在不计入缓存 /// 如果不存在则放入Cookie中 /// 在数据库持久化 /// redis: /// sessions: /// session1 /// session2 /// pvs: /// sessionId1 :accessPath1 /// sessionId2 :accessPath2 /// uvs: /// path1: 1 /// path2: 1 /// 首先记录pv /// 然后判断请求中是否携带sesionId /// false:生成并返回、记录sessionId和uv /// true:判断该sessionId是否存在于sessions中 /// true: /// false:记录sessionId和uv /// sessionId :accessPath string Path = httpContext.Request.Path; /*string sessionId; * if (!httpContext.Request.Cookies.ContainsKey("session-id")) * { * ISession session = httpContext.Session; * httpContext.Response.Cookies.Append("session-id", session.Id); * sessionId = session.Id; * } else * { * sessionId = httpContext.Request.Cookies["session-id"]; * } * * this.addTotal(sessionId); * this.addSpecial(sessionId,Path);*/ /*if(!httpContext.Request.Cookies.ContainsKey("session-id")) * { * ISession session = httpContext.Session; * httpContext.Response.Cookies.Append("session-id", session.Id); * _redis.ListRightPush("sessions", session.Id); * * RedisValue path = _redis.StringGet(Path); * if (path.IsNullOrEmpty) * { * _redis.StringSet(Path, 0); * } * _redis.StringIncrement(Path, 1); * } else * { * Boolean isExists = false; * RedisValue[] sessions = _redis.ListRange("sessions"); * if (sessions.Length != 0) * { * for (int i = 0; i < sessions.Length; i++) * { * isExists = httpContext.Request.Cookies.TryGetValue("session-id",out string v); * } * } * * if(!isExists) * { * RedisValue path = _redis.StringGet(Path); * _redis.ListRightPush("sessions", httpContext.Request.Cookies["session-id"]); * if (path.IsNullOrEmpty) * { * _redis.StringSet(Path, 0); * } * _redis.StringIncrement(Path, 1); * } * }*/ _accessHistoryLog = new AccessHistoryLog(); _accessHistoryLog.IpAddress = httpContext.Connection.RemoteIpAddress.ToString(); _accessHistoryLog.DateTime = DateTime.Now; _accessHistoryLog.AccessPath = Path; _context.AccessHistoryLogs.Add(_accessHistoryLog); try { _context.SaveChanges(); } catch { return(null); } return(_accessHistoryLog); }