ashx中使用session的方法(获取session值)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ashx中使用session的方法(获取session值),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2148字,纯文字阅读大概需要4分钟。
内容图文
![ashx中使用session的方法(获取session值)](/upload/InfoBanner/zyjiaocheng/379/7480007a6d4f4a18b0e0c826349f64e7.jpg)
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;
}
}
}
}
内容总结
以上是互联网集市为您收集整理的ashx中使用session的方法(获取session值)全部内容,希望文章能够帮你解决ashx中使用session的方法(获取session值)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。