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 }
  • 相关阅读:
    构建之法阅读笔记03
    构建之法阅读笔记01
    构建之法阅读笔记02
    周总结06
    《大道至简》第一章伪代码
    《大道至简》观后感
    【leetcode】Valid Number
    【leetcode】Maximal Rectangle
    【Unity3D】Invoke,InvokeRepeating ,Coroutine 延迟调用,周期性调用
    【leetcode】Scramble String
  • 原文地址:https://www.cnblogs.com/liqingwen/p/6131658.html
Copyright © 2011-2022 走看看