Asp.net Ajax framework

Source: Internet
Author: User

1. Change the method name corresponding to the server method on the client
During development, you may encounter the following problem: the server exposes two methods, one of which is to overload the other. That is, the two methods have the same name, but the method signature is different. At this point, we will find that although the server exposes two methods, we can only use one of them. When you view the automatically generated JS proxy, you will find that, only one method proxy is generated on the page. Why is there such a problem?
When the Asp.net Ajax framework generates a proxy for a server method, It stores the method information in a JSON dictionary. The method name is the dictionary key and value is other information of the method. When two methods with the same name (that is, the same key) are met, the last operation on the same key overwrites the previous operation on this key. Therefore, when the agent is generated, only one method proxy will be generated.
The method to solve this problem is simple, that is, when writing the server method, you can specify the corresponding method name to be generated to the client in the webmethod constructor, that is
[System. Web. Services. webmethod]
Public static int getrandom ()
{
Random random = new random (datetime. Now. millisecond );
Return random. Next ();
}
[System. Web. Services. webmethod (messagename = "getrangrandom")]
Public static int getrandom (INT begin, int end)
{
Random random = new random (datetime. Now. millisecond );
Return random. Next (begin, end );
}
The following is a complete example:
<% @ Page Language = "C #" %>
<% @ Register Assembly = "system. Web. Extensions, version = 1.0.61025.0, culture = neutral, publickeytoken = 31bf3856ad364e35"
Namespace = "system. Web. UI" tagprefix = "asp" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<SCRIPT runat = "server">
[System. Web. Services. webmethod]
Public static int getrandom ()
{
Random random = new random (datetime. Now. millisecond );
Return random. Next ();
}
[System. Web. Services. webmethod (messagename = "getrangrandom")]
Public static int getrandom (INT begin, int end)
{
Random random = new random (datetime. Now. millisecond );
Return random. Next (begin, end );
}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> change the method name of the server on the client </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: scriptmanager id = "scriptmanager1" enablepagemethods = "true" runat = "server">
</ASP: scriptmanager>
<Script language = "JavaScript" type = "text/JavaScript">
Pagemethods. set_timeout (5000 );
Pagemethods. set_defaultfailedcallback = "onfailed ";
Function getrandom (begin, end)
{
If (arguments. length! = 2)
{
Pagemethods. getrandom (onsucceed );
}
Else
{
Pagemethods. getrangrandom (begin, end, onsucceed );
}
}
Function onsucceed (Message)
{
Alert (Message );
}
Function onfailed (errorhander)
{
VaR errormessage = 'timeout + errorhander. get_timedout () + '\ n error message:' + errorhander. get_message ();
Errormessage = errormessage + '\ n Exception name:' + errorhander. get_exceptiontype () + '\ n error location:' + errorhander. get_stacktrace ();
Alert (errormessage );
}
</SCRIPT>
<Input type = "button" value = "getrandom" onclick = "getrandom ()"/>
<Input type = "button" value = "getrangrandom" onclick = "getrandom (50,100)"/>
</Div>
</Form>
</Body>
</Html>
Ii. Change the client access method to the server
By default, the http post method is used when the client accesses the server in Asp.net Ajax. If you want to access the server through http get, you can do this:
[System. Web. Services. webmethod (messagename = "getrangrandom")]
[System. Web. Script. Services. scriptmethod (usehttpget = true)]
Public static int getrandom (INT begin, int end)
{
Random random = new random (datetime. Now. millisecond );
Return random. Next (begin, end );
}
In this way, when the client calls the getrandom (INT begin, int end) method, it is called through http get.
3. Use session in Asp.net Ajax
[System. Web. Services. webmethod (messagename = "getrangrandom")]
[System. Web. Script. Services. scriptmethod (usehttpget = true)]
[System. Web. Services. webmethod (enablesession = true)]
Public static int getrandom (INT begin, int end)
{
Random random = new random (datetime. Now. millisecond );
Return random. Next (begin, end );
}
[System. Web. Services. webmethod (enablesession = true)] identifies a server method to enable session.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.