aspx与ashx,asmx之间的参数传递与调用

来源:互联网 发布:中世纪2优化9波兰 编辑:程序博客网 时间:2024/05/02 02:12


1、创建一个空web工程;

2、添加一个一般处理程序;

3、添加一个html类;

4、添加一个webservice;


测试1:  html加载ashx实现信息显示;效果等同于aspx的显示,区别:点击按钮后aspx方式仍保留输入的信息;

<body>
    <form action="RequestHandler1.ashx" method="get">
        <input type="text" name="text" />
        <input id="bt" type="submit" value="发送请求" />
    </form>
</body> 


//处理程序

     public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string str = context.Request["text"];
            context.Response.Write("返回值是:" + str);


            //string fullpath = context.Server.MapPath("HtmlPage1.html"); //加载全文
            //string content = File.ReadAllText(fullpath);
            //context.Response.Write(content);
        }

测试二:webForm调用webService内的处理方法;

       webForm后台程序:

        public WebService1 f = new WebService1();  //调用未发布的webservice,创建对象
        //WebReference.Service se = new WebReference.Service();  //实例化要调用的 webservice对象,服务引用方式

       protected void change_Click(object sender, EventArgs e)
        {
            string n = f.ReverseString(this.lbl1.Text);
            this.lbl2.Text = n;
        }

WebService1 代码:

     [WebMethod]
        public string ReverseString(string message)  //反转字符串
        {
            char[] arr = message.ToCharArray();
            Array.Reverse(arr);
            message = new string(arr);
            return message;
        }
       

测试三:aspx向ashx传递参数,同时加载ashx处理函数;

    protected void change_Click(object sender, EventArgs e)
        {
            Response.Redirect("JsonHandler1.ashx?Age=20");
        }

一般处理程序接收参数,执行处理过程:

   public class JsonHandler1 : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string age =  context.Request.QueryString["Age"].ToString();

             YourClass obj = new YourClass();
                //给对象赋值
                obj.Name = "三";
                obj.Age = Convert.ToInt32( age);
                ///返回文件实体的JSON表示形式,用Newtonsoft.Json库
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
                context.Response.Write(json); //直接返回到界面上
                context.Response.Flush();
            
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public class YourClass
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }

测试四:aspx调用ashx,ashx向aspx传递参数,结果显示早指定位置;

webForm程序:

        protected void Page_Load(object sender, EventArgs e)
        {
            string ss = Request.QueryString["str"];  
            this.lbl3.Text = ss;  

}

        protected void change_Click(object sender, EventArgs e)
        {
            Response.Redirect("JsonHandler1.ashx?Age=20");
        }

ashx程序:

         public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string age =  context.Request.QueryString["Age"].ToString();
      
                YourClass obj = new YourClass();
                //给对象赋值
                obj.Name = "三";
                obj.Age = Convert.ToInt32( age);
                ///返回文件实体的JSON表示形式,用Newtonsoft.Json库
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
                HttpContext.Current.Server.Execute("WebForm1.aspx?str=" + json);
}

测试五:aspx前端调用ashx,ashx向aspx传递参数,结果显示早指定位置;

  <div> <a href="JsonHandler1.ashx?Age=20">一般处理程序</a>  </div>


测试六:aspx前端采用ajax方式调用处理程序,将结果返回至ajax方法;

   var sid = document.getElementById("<%=ddlSty.ClientID%>").value;
        var startTime = document.getElementById("<%=tbStartTime.ClientID%>").value;
        var endTime = document.getElementById("<%=tbEndTime.ClientID%>").value;
        //alert("sid数据" + sid);
        $.ajax({
            type: "post",
            async: false, //同步执行
            url: "GroupJsonDataServer.ashx",
            data: { type: "getGroupGainWeight", SID: sid, StartTime: startTime, EndTime: endTime },


            dataType: "json", //返回数据形式为json
            success: function (result) {
                if (result) {
                    //将返回的category和series对象赋值给options对象内的category和series
                    //因为xAxis是一个数组 这里需要是xAxis[i]的形式
                    option2.xAxis[0].data = result.category;
                    option2.series = result.series;
                    //option2.legend.data = result.legend;
                    myChart2.hideLoading();
                    myChart2.setOption(option2, true);//属性设为true表示数据不合并显示
                }
            },
            error: function (errorMsg) {
                alert("不好意思,图表请求数据失败啦!");
            }
        });


处理函数程序:

  private HttpContext CurContext = null;
        public void ProcessRequest(HttpContext context)
        {
            CurContext = context;
            context.Response.ContentType = "text/plain";

            string type = context.Request.Form["type"];
            string sid = context.Request.Form["SID"];
            string startTime = context.Request.Form["StartTime"];
            string endTime = context.Request.Form["EndTime"];

            if (!string.IsNullOrEmpty(type))
            {
                switch (type)
                {                  
                    case "getGroupGainWeight":
                          GroupGainWeight(sid, startTime, endTime);//群体增重
                        break;

default : break;

                }
            }
        }



0 0