public static void SetCookies(string key, string value, int minutes = 999999) { MvcContext.GetContext().Response.Cookies.Append(key, value, new CookieOptions() { Expires = DateTime.Now.AddMinutes(minutes), IsEssential = true }); }
/// <summary> /// 获取cookies /// </summary> /// <param name="key">键</param> /// <returns>返回对应的值</returns> public static string GetCookies(string key) { MvcContext.GetContext().Request.Cookies.TryGetValue(key, out string value); if (string.IsNullOrEmpty(value)) { value = string.Empty; } return(value); }
public static dynamic GetCurrentCountryAndLang() { try { if (MvcContext.GetContext().Request.Path.Value.ToLower().ToString().Contains("/home/error")) { return(new { Country = "us", Language = "en" }); } //string regex = @"(?<=^/)([x]{1}|[a-z]{2,})_(([\w-]+(?=/))|[\w-]{5})"; string regex = @"(?<=^/)([x]{1}|[a-z]{2,})_(([\w-]{2}-[\w-]{2})|([\w-]{2,3}))"; Match lac = Regex.Match(MvcContext.GetContext().Request.Path, regex);//获取到国家和语言 string regexalc = @"[^_]+"; var lacarr = Regex.Matches(lac.ToString(), regexalc); return(new { Country = lacarr[0].ToString().ToLower(), Language = lacarr[1].ToString().ToLower() }); } catch (Exception ex) { LogHelper.Error("获取地址栏国家和语言出错" + ex.Message + ex.StackTrace + "当前地址:" + MvcContext.GetContext().Request.Host + MvcContext.GetContext().Request.Path); return(new { Country = "us", Language = "en" }); } }
/// <summary> /// 删除指定的cookie /// </summary> /// <param name="key">键</param> public static void DeleteCookies(string key) { MvcContext.GetContext().Response.Cookies.Delete(key); }