C#程序中session的基本設(shè)置示例及清除session的方法
更新時(shí)間:2016年04月05日 16:41:03 作者:sunzhenlin2008
這篇文章主要介紹了C#程序中session的基本設(shè)置示例及清除session的方法,是C#入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
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
//清除某個(gè)Session
Session["UserName"] = null;
Session.Remove("UserName");
相關(guān)文章
unity 如何使用文件流讀取streamingassets下的資源
這篇文章主要介紹了unity 使用文件流讀取streamingassets下的資源操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
C#?OpenCV實(shí)現(xiàn)形狀匹配的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#+OpenCV實(shí)現(xiàn)形狀匹配的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09
C#?winform實(shí)現(xiàn)中英文切換功能的四種方式
這篇文章主要介紹了在C#?winform應(yīng)用程序中實(shí)現(xiàn)中英文切換功能的四種方式,資源文件(Resources),本地化(Localization),動(dòng)態(tài)設(shè)置控件字體和切換語(yǔ)言環(huán)境這四種方式,下面將詳細(xì)介紹每種方式及其具體實(shí)現(xiàn),并討論它們的優(yōu)缺點(diǎn),需要的朋友可以參考下2024-04-04
分析C# Dictionary的實(shí)現(xiàn)原理
對(duì)于C#中的Dictionary類相信大家都不陌生,這是一個(gè)Collection(集合)類型,可以通過(guò)Key/Value(鍵值對(duì)的形式來(lái)存放數(shù)據(jù);該類最大的優(yōu)點(diǎn)就是它查找元素的時(shí)間復(fù)雜度接近O(1)。那么什么樣的設(shè)計(jì)能使得Dictionary類實(shí)現(xiàn)O(1)的時(shí)間復(fù)雜度呢2021-06-06
C#監(jiān)測(cè)IPv4v6網(wǎng)速及流量的實(shí)例代碼
這篇文章主要介紹了C#監(jiān)測(cè)IPv4v6網(wǎng)速及流量的實(shí)例代碼,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07

