zoukankan      html  css  js  c++  java
  • [C#] 简单的 Helper 封装 -- CookieHelper

     1 using System;
     2 using System.Web;
     3 
     4 namespace ConsoleApplication5
     5 {
     6     /// <summary>
     7     /// Cookie 助手
     8     /// </summary>
     9     public sealed class CookieHelper
    10     {
    11         /// <summary>
    12         /// 添加一个 Cookie
    13         /// </summary>
    14         /// <param name="name"></param>
    15         /// <param name="value"></param>
    16         public static void Add(string name, string value)
    17         {
    18             var cookie = new HttpCookie(name, value);
    19 
    20             HttpContext.Current.Response.Cookies.Add(cookie);
    21         }
    22 
    23         /// <summary>
    24         /// 添加一个 Cookie
    25         /// </summary>
    26         /// <param name="name"></param>
    27         /// <param name="value"></param>
    28         /// <param name="expires">过期日期和时间</param>
    29         public static void Add(string name, string value, DateTime expires)
    30         {
    31             var cookie = new HttpCookie(name, value)
    32             {
    33                 Expires = expires
    34             };
    35 
    36             HttpContext.Current.Response.Cookies.Add(cookie);
    37         }
    38 
    39         /// <summary>
    40         /// 获取 Cookie 值
    41         /// </summary>
    42         /// <param name="name"></param>
    43         /// <returns></returns>
    44         public static string Get(string name)
    45         {
    46             var cookie = HttpContext.Current.Request.Cookies[name];
    47 
    48             return cookie == null ? string.Empty : cookie.Value;
    49         }
    50     }
    51 }
  • 相关阅读:
    北京联通光猫WO-36(HG220GS-U)改为桥接模式
    使用DataGrip导入数据
    Spring知识点
    mybatis-generator
    项目中mybatis连接mysql常见问题
    Spring AOP
    Volatile
    Redis知识点
    Spring IOC
    Observer模式
  • 原文地址:https://www.cnblogs.com/liqingwen/p/6131658.html
Copyright © 2011-2022 走看看