C#程序中session的基本設(shè)置示例及清除session的方法
更新時間:2016年04月05日 16:41:03 作者:sunzhenlin2008
這篇文章主要介紹了C#程序中session的基本設(shè)置示例及清除session的方法,是C#入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
session的基本設(shè)置:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace OAFrameWork
{
public class CSession
{
public static object Get(string Key)
{
return HttpContext.Current.Session[Key];
}
public static string GetString(string Key)
{
object obj = HttpContext.Current.Session[Key];
if (obj == null) return "";
else return obj.ToString();
}
public static object Get(string Key,object DefaultValue)
{
if (HttpContext.Current.Session[Key] == null)
return DefaultValue;
else
return HttpContext.Current.Session[Key];
}
public static object Get(string Key, object DefaultValue,Boolean CanAdd)
{
if (HttpContext.Current.Session[Key] == null)
{
if(CanAdd==true)
HttpContext.Current.Session.Add(Key, DefaultValue);
return DefaultValue;
}
else
return HttpContext.Current.Session[Key];
}
public static Boolean Set(string Key,object Value)
{
try
{
if (Value == null && HttpContext.Current.Session[Key] != null)
{
HttpContext.Current.Session.Remove(Key);
}
else if (HttpContext.Current.Session[Key] == null)
{
HttpContext.Current.Session.Add(Key, Value);
}
else
{
HttpContext.Current.Session[Key] = Value;
}
return true;
}
catch (Exception ex)
{
CMsgBox.Show(ex.Message);
return false;
}
}
}
}
清除Session:
Session.Abandon();//清除全部Session
//清除某個Session
Session["UserName"] = null;
Session.Remove("UserName");
相關(guān)文章
unity 如何使用文件流讀取streamingassets下的資源
這篇文章主要介紹了unity 使用文件流讀取streamingassets下的資源操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
C#?winform實現(xiàn)中英文切換功能的四種方式
這篇文章主要介紹了在C#?winform應(yīng)用程序中實現(xiàn)中英文切換功能的四種方式,資源文件(Resources),本地化(Localization),動態(tài)設(shè)置控件字體和切換語言環(huán)境這四種方式,下面將詳細介紹每種方式及其具體實現(xiàn),并討論它們的優(yōu)缺點,需要的朋友可以參考下2024-04-04
C#監(jiān)測IPv4v6網(wǎng)速及流量的實例代碼
這篇文章主要介紹了C#監(jiān)測IPv4v6網(wǎng)速及流量的實例代碼,文中講解非常細致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07

