使用HTTP GET访问方法

•使用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的方法访问

你可能感兴趣的:(http)