of the method that USES session in ashx gets the session value
- 2020-10-23 20:56:34
- OfStack
WEB development makes it easy to get Request and Response objects in general handler 1, such as:
HttpRequest _request = context.Request;
HttpResponse _response = context.Response;
But it's not that easy to get the value of Session.
For example, if you want to get the login user information saved in Session Session["LoginUser"]
If you use context.Session ["LoginUser"] only, you will report an exception that says "no object reference was set to an instance of the object"!
Specifically, the following methods should be used:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace DtlCalendar.Mobile.Site.Manage
{
/// <summary>
/// DelApk Summary description of
/// </summary>
public class DelApk : IHttpHandler, IReadOnlySessionState
{
// IReadOnlySessionState : Read-only access Session
// IRequiresSessionState : Read and write access 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;
}
}
}
}