脚本宝典收集整理的这篇文章主要介绍了ashx中使用session的方法(获取session值),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
WEB开发,在一般处理程序中,很容易得到 Request和Response对象,如:
但是要得到 Session的值就没有那么简单了。
比如如果要在ashx得到保存在Session中的登录用户信息 Session["LoginUser"]
如果仅仅使用 context.Session["LOGinUser"] 的话,是会报 “未将对象引用设置到对象的实例”的异常!
具体要使用下列方法:
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值)所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。