使用HTTP GET访问方法

本文介绍如何在ASP.NET AJAX中使用ScriptMethodAttribute标记实现GET请求方式调用WebService服务方法,并提供了一个具体的示例,包括客户端和服务端的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

•使用ScriptMethodAttribute进行标记
–UseHttpGet属性设为true
•客户端使用代理的方法没有任何变化
•参数将使用QueryString进行传递
•性能较HTTPPOST方法略有提高
•一些特性略有改变
–缓存的基础


aspx
    <form id="form1" runat="server">
        
<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug">
            
<Services>
                
<asp:ServiceReference Path="Services/UseHttpGetService.asmx" InlineScript="true" />
            
</Services>
        
</asp:ScriptManager>
        
        
<input type="button" value="Get Random" onclick="getRandom()" />
        
<input type="button" value="Get Range Random" onclick="getRandom(50, 100)" />
        
        
<script language="javascript" type="text/javascript">
            function getRandom(minValue, maxValue)
            {
                
if (arguments.length != 2)
                {
                    UseHttpGetService.GetRandom(onSucceeded);
                }
                
else
                {
                    UseHttpGetService.GetRangeRandom(minValue, maxValue, onSucceeded);
                }
            }
            
            function onSucceeded(result)
            {
                alert(result);
            }
        
</script>
    
</form>

UseHttpGetService.asmx
<%@ WebService Language="C#" Class="UseHttpGetService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

[WebService(Namespace 
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class UseHttpGetService  : System.Web.Services.WebService
{
    [WebMethod]
    
public int GetRandom()
    {
        
return new Random(DateTime.Now.Millisecond).Next();
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet
=true)]
    
public int GetRangeRandom(int minValue, int maxValue)
    {
        
return new Random(DateTime.Now.Millisecond).Next(minValue, maxValue);
    }
}
WebService方法加上    [ScriptMethod(UseHttpGet=true)]修饰即表示使用get的方法访问

转载于:https://www.cnblogs.com/timy/archive/2008/04/30/1178267.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值