在asp.net handler 中 使用 session
2016-01-13 17:19
676 查看
首先 handler 默認情況下是不支持 session 的。如果想在一般處理程序(handler)中使用session ,需要實現一個不需要實現任何方法的接口IRequiresSessionState
引入 System.Web.SessionState 即可。此時需要context.Session["pars"]這樣引用。具體實例如下圖所示:
下面是網友的文章:
參考文章1
原文鏈接
抽象类
[csharp] view
plaincopyprint?
using System;
using System.Web;
using System.Web.SessionState;
....
namespace SRERC.Web.admin
{
/// <summary>
/// SessionAwareHandler 的摘要说明
/// </summary>
public abstract class SessionAwareHandler : IHttpHandler, IRequiresSessionState
{
.....
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
// 身份认证
// 权限控制
MyProcess(context);
}
........
protected abstract void MyProcess(HttpContext context);
}
}
一般处理程序实现类:
[csharp] view
plaincopyprint?
using System;
using System.Web;
namespace SRERC.Web.admin
{
/// <summary>
/// bandHandler 的摘要说明
/// </summary>
public class bandHandler : SessionAwareHandler
{
protected override void MyProcess(HttpContext context)
{
//....处理过程
//....
context.Response.Write(json);
}
}
}
參考文章2 stackoverflow
原文鏈接
原文中採納的回復就是正解
Implement the System.Web.SessionState.IRequiresSessionState interface
OK,能在handler中使用session了。但是不知道是否符合規矩
引入 System.Web.SessionState 即可。此時需要context.Session["pars"]這樣引用。具體實例如下圖所示:
下面是網友的文章:
參考文章1
原文鏈接
抽象类
[csharp] view
plaincopyprint?
using System;
using System.Web;
using System.Web.SessionState;
....
namespace SRERC.Web.admin
{
/// <summary>
/// SessionAwareHandler 的摘要说明
/// </summary>
public abstract class SessionAwareHandler : IHttpHandler, IRequiresSessionState
{
.....
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
// 身份认证
// 权限控制
MyProcess(context);
}
........
protected abstract void MyProcess(HttpContext context);
}
}
一般处理程序实现类:
[csharp] view
plaincopyprint?
using System;
using System.Web;
namespace SRERC.Web.admin
{
/// <summary>
/// bandHandler 的摘要说明
/// </summary>
public class bandHandler : SessionAwareHandler
{
protected override void MyProcess(HttpContext context)
{
//....处理过程
//....
context.Response.Write(json);
}
}
}
參考文章2 stackoverflow
原文鏈接
原文中採納的回復就是正解
Implement the System.Web.SessionState.IRequiresSessionState interface
public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Session["StackOverflow"] = "overflowing"; context.Response.Redirect("~/AnotherPage.aspx"); } }
OK,能在handler中使用session了。但是不知道是否符合規矩
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- kindeditor 批量上传 上传失败 thinkphp swfupload session
- 杰奇登录后的东西都是在session里面的
- 星外ASP.Net的安全设置相关说明
- C#、ASP.NET通用扩展工具类之TypeParse
- ASP中SESSION无法保存问题的解决办法
- 实现ASP.NET无刷新下载并提示下载完成的开发思路
- C#、ASP.NET通用扩展工具类之LogicSugar
- C#、ASP.NET通用工具类IsWhat?(可以判断数字、身份证、数据类型等等)
- Oracle中的Connect/session和process的区别及关系介绍
- 程序中常用的种代码
- Node.js编程中客户端Session的使用详解
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- ASP.NET页面间的传值的几种方法
- 浅谈COOKIE和SESSION区别
- 入侵ASP.net网站的经验附利用代码
- 解析PHP的session过期设置
- php中Session的生成机制、回收机制和存储机制探究
- php中将一个对象保存到Session中的方法