在使用Ajax技术时,被调用页面只需要返回相应字符/数据就可以了,所以我们可以使用ashx文件来代替aspx,在为了方便代码的转换,我们只需要在ProcessRequest中添加如下几句就可以了。
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
'通用替换
Dim Request As HttpRequest = context.Request
Dim Response As HttpResponse = context.Response
Dim Session As HttpSessionState = context.Session
Dim server As HttpServerUtility = context.Server
'----------------------------
‘你的代码
end sub
如果需要使用session,则需要引入:
Imports System.Web.SessionState
并在类定义中加入借口引用
Public Class RandomImage : Implements IHttpHandler, IRequiresSessionState
补充材料:
在一般事务处理页面,可以轻松的得到 Request,Response对象,从而进行相应的操作,如下:
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
但是要得到 Session的值就没有那么简单了。比如你要在ashx得到保存在Session中的登录帐号Session["userAccount"]
如果你只是context.Session["userAccount"]的话是会报 “未将对象引用设置到对象的实例”的异常
所以,如果要想取Session中的值 ,需要如下所示
1、引入 命名空间:
using System.Web.SessionState;
2、实现IRequiresSessionState接口,具体如下
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AddUserInfo : IHttpHandler,IRequiresSessionState//就是这样显示的实现一下,不用实现什么方法
{
public void ProcessRequest(HttpContext context)
{
//...
//这样你就可以如下 操作了
if(context.Session["userAccount"] != null)
{
string account = context.Session["userAccount"].ToString();
}
//...继续下面的代码
}
}