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.Aount;
return true;
}
}
user = string.Empty;
return false;
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指