一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

ajax之aspx或ashx页面里进行多次使用

时间:2011-12-02 编辑:简简单单 来源:一聚教程网

前台页面AjaxTest.htm,内容如下

 代码如下 复制代码

http://www.111com.net>



    本页用不同的方式与后台进行交互
   
   


后台建立一个Handler.ashx页面 内容如下

 代码如下 复制代码

<%@ WebHandler Language="C#" class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
 
        if (context.Request["command"]!=null)
           
        {

                //得到前台传过来的command,确定调用哪个方法


           string command = context.Request["command"].ToString();
          string data = context.Request["value"].ToString();
         switch (command)
          {
               case "method1":
                 method1(context);
                  break;
              case "method2":
                  method2(context);
                  break;
               default:
                 break;
           }
        }
             
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    public void method1(HttpContext context)
    {
      
        context.Response.Write("hello,"+context.Request["value"].ToString());
      
      
    }
    public void method2(HttpContext context)
    {
               context.Response.Write("hello,"+context.Request["value"].ToString());
    }

 

}

如果有多个方法,switch  case里的判断将会很多.考虑用更简单的方法.使用反射

 代码如下 复制代码

<%@ WebHandler Language="C#" class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        if (context.Request["command"] != null)
        {

         //
            string command = context.Request["command"].ToString();
            System.Reflection.MethodInfo method = this.GetType().GetMethod(command);
            if (method != null)
            {
                method.Invoke(this, new object[] { context});
            }
        }
       
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    public void method1(HttpContext context)
    {
 
        context.Response.Write("hello"+context.Request["value"].ToString());
      
      
    }
    public void method2(HttpContext context)
    {

        context.Response.Write("hello,"+context.Request["value"].ToString());
    }

 

}

  使用反射大大简化了程序.

=====================================================

使用aspx页面与ajax交互

  新建一个aspx页面 WebMethod.aspx

 将WebMethod.aspx页里的多余部分删除,只保留

   

 代码如下 复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebMethod.aspx.cs" Inherits="WebMethod" %>

这一条语句

WebMethod.aspx.cs内容如下

 代码如下 复制代码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Web.Services;
using System.Reflection;

public partial class WebMethod : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string methodName = HttpContext.Current.Request.PathInfo.Substring(1);
      // Response.Write(methodName);
        MethodInfo method = this.GetType().GetMethod(methodName);
        if (method != null)
        {
            Response.Write(method.Invoke(this,new object[]{}));
        }
       // Response.Write(GetResult());
      
    }
    [WebMethod(EnableSession=true)]
    public  string GetResult()
    {
        //return "hello";
        if (HttpContext.Current.Request["name"] != null)
        {
            string value = HttpContext.Current.Request["name"].ToString();
            //HttpContext.Current.Request.PathInfo;
            return "{'name':'"+value+"'}";
        }
        else
        {
            return "{name:'error'}";
        }
    }
}

test.html页面与WebMethod.aspx页面进行ajax交互 test.html页面内容



    使用aspx页面进行交互
   
   


热门栏目