using System;
using System.Web;
namespace FineAdmin.Common
{
public class WebHelper
{
#region Session操作
///
/// 写Session
///
/// Session键值的类型
/// Session的键名
/// Session的键值
public static void WriteSession(string key, T value)
{
if (key.IsEmpty())
return;
HttpContext.Current.Session[key] = value;
}
///
/// 写Session
///
/// Session的键名
/// Session的键值
public static void WriteSession(string key, string value)
{
WriteSession(key, value);
}
///
/// 读取Session的值
///
/// Session的键名
public static string GetSession(string key)
{
if (key.IsEmpty())
return string.Empty;
return HttpContext.Current.Session[key] as string;
}
///
/// 删除指定Session
///
/// Session的键名
public static void RemoveSession(string key)
{
if (key.IsEmpty())
return;
HttpContext.Current.Session.Contents.Remove(key);
}
#endregion
#region Cookie操作
///
/// 写cookie值
///
/// 名称
/// 值
public static void WriteCookie(string strName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
}
///
/// 写cookie值
///
/// 名称
/// 值
/// 过期时间(分钟)
public static void WriteCookie(string strName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
cookie.Expires = DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);
}
///
/// 读cookie值
///
/// 名称
/// cookie值
public static string GetCookie(string strName)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
{
return HttpContext.Current.Request.Cookies[strName].Value.ToString();
}
return "";
}
///
/// 删除Cookie对象
///
/// Cookie对象名称
public static void RemoveCookie(string CookiesName)
{
HttpCookie objCookie = new HttpCookie(CookiesName.Trim());
objCookie.Expires = DateTime.Now.AddYears(-5);
HttpContext.Current.Response.Cookies.Add(objCookie);
}
#endregion
}
}