ashx中使用session的方法(获取session值)
WEB开发,在一般处理程序中,很容易得到 Request和Response对象,如:
HttpRequest _request = context.Request;
HttpResponse _response = context.Response;
但是要得到 Session的值就没有那么简单了。
比如如果要在ashx得到保存在Session中的登录用户信息 Session["LoginUser"]
如果仅仅使用 context.Session["LoginUser"] 的话,是会报 “未将对象引用设置到对象的实例”的异常!
具体要使用下列方法:
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;
}
}
}
}
相关文章
如何在 ASP.NET Core Web API 中处理 Patch 请求
这篇文章主要介绍了在 ASP.NET Core Web API中处理Patch请求,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-05-05
如何传值在2个页面之间 要求不刷新父页面,并且不能用Querystring传值
通过Cookie,因为它既可以在服务器端对其进行操作,也可在客户端对其进行操作但是缺点是不安全,而且有时客户端会由于安全问题禁用Cookie!2008-12-12
Win2008 server + IIS7 设置身份模拟(ASP.NET impersonation)
IIS7 与 IIS 6 相比有了很大的改动,原来在 IIS 6 下可以的设置到了 IIS 7 下有的会发生变化。身份模拟的配置上,IIS7 和 IIS6有很大不同,网上IIS6的身份模拟的文章比较多,但介绍IIS7的比较少,我把的一些折腾的经验在这篇博客中写下来,以供参考2011-10-10
ASP.NET MVC HttpPostedFileBase文件上传的实例代码
这篇文章主要介绍了ASP.NET MVC HttpPostedFileBase文件上传,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-07-07
ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC
同一相站点,有些页面的客户端验证能工作,而有些死活不行。打开页面就出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null错误2010-04-04


最新评论