ashx中使用session的方法(獲取session值)
WEB開發(fā),在一般處理程序中,很容易得到 Request和Response對象,如:
HttpRequest _request = context.Request;
HttpResponse _response = context.Response;
但是要得到 Session的值就沒有那么簡單了。
比如如果要在ashx得到保存在Session中的登錄用戶信息 Session["LoginUser"]
如果僅僅使用 context.Session["LoginUser"] 的話,是會報 “未將對象引用設(shè)置到對象的實例”的異常!
具體要使用下列方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace DtlCalendar.Mobile.Site.Manage
{
/// <summary>
/// DelApk 的摘要說明
/// </summary>
public class DelApk : IHttpHandler, IReadOnlySessionState
{
// IReadOnlySessionState :只讀訪問Session
// IRequiresSessionState :讀寫訪問Session
public void ProcessRequest(HttpContext context)
{
string strID = context.Request["id"];
context.Response.Clear();
context.Response.ContentType = "text/plain";
int id;
string user;
if (int.TryParse(strID, out id) && IsLoged(context, out user))
{
string reslt = DataProvider.MobileDataProvider.CreateInstance().DelMApk(id).ToString();
BLL.LogOprHelper.Instance.InsertMLog(user, BLL.LogOpr.Delete, "DelApk result:" + reslt);
context.Response.Write(reslt);
}
else
{
BLL.LogOprHelper.Instance.InsertMLog(strID, BLL.LogOpr.Delete, "DelApk result:-1");
context.Response.Write("-1");
}
}
private bool IsLoged(HttpContext context, out string user)
{
BLL.User _User;
if (context.Session["LoginUser"] != null)
{
_User = context.Session["LoginUser"] as BLL.User;
if (_User != null)
{
user = _User.Account;
return true;
}
}
user = string.Empty;
return false;
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
相關(guān)文章
ASP.NET Core依賴注入系列教程之控制反轉(zhuǎn)(IoC)
這篇文章主要給大家介紹了關(guān)于ASP.NET Core依賴注入系列教程之控制反轉(zhuǎn)(IoC)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11ASP.NET GridView 實現(xiàn)課程表顯示(動態(tài)合并單元格)實現(xiàn)步驟
GridView,ASP.NET中很常用的數(shù)據(jù)顯示控件,這里,我將用這個控件來實現(xiàn)課程表的顯示。首先說說課程表的顯示與普通記錄的顯示有何不同?感興趣的朋友可以了解下,或許對你有所幫助2013-02-02Asp.net MVC中使用JQuery插件ajaxFileUpload上傳文件
這篇文章主要介紹了Asp.net MVC中使用JQuery插件ajaxFileUpload上傳文件,需要的朋友可以參考下2016-08-08VS2022?.NET5一鍵發(fā)布到遠(yuǎn)程騰訊云IIS服務(wù)器的詳細(xì)步驟
這篇文章主要介紹了VS2022?.NET5一鍵發(fā)布到遠(yuǎn)程騰訊云IIS服務(wù)器,首先需要添加服務(wù)器相關(guān)功能,文中通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04ASP.NET中實現(xiàn)Form表單字段值自動填充到操作模型中
這篇文章主要介紹了ASP.NET中實現(xiàn)Form表單字段值自動填充到操作模型中,本文模仿MVC模式中的自動映射表單了模型,使用泛型和反射實現(xiàn),需要的朋友可以參考下2015-06-06