Asp. NET using WebMethod

Source: Internet
Author: User
Tags string format

The method of the normal page is advertised as WebMethod and accessed in JavaScript form.

1 method to public static decoration, the return type is preferably a string.
The [WebMethod] attribute is added before the 2 method.
3 client-side access to use the Post method, and JSON as the data form of interaction. Otherwise, the entire page of HTML is returned.
4 when jquery accesses, the DATA.D in the callback is the true return content.
5 The Access URL is: http://abc.com/abc.aspx/GetTime if there is a GetTime public static method.

Cases:
Abc.aspx
[WebMethod]
public static string GetTime ()
{
Return DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss");
}
---------------
Script (called in jquery for example)
$.ajax ({
Url:url,
Method: "Post",
DataType: "JSON",
ContentType: "Application/json; Charset=utf-8 ",
Success:function (data) {
$ ("#id"). HTML (DATA.D); See 3rd
}
});


Get rid of ashx/asmx, use Jquery.ajaxwebservice request Webmethod,ajax processing more concise
A generic handler (*.ASHX) or Web service (*.asmx), and each Ajax request is built with one such file, so that if there's more Ajax programs in a project, There is bound to be a bunch of. ashx or. asmx, although the program itself is not a hindrance, but that pile of files seems to always feel very hurt. So can you throw away these. ashx and. asmx, choose a more concise way to do AJAX programs.
The answer is yes, that is: WebMethod.
First, create a public static method in the Aspx.cs file, and then add the WebMethod property.
Such as:

[WebMethod]
public static string GetUserName ()
{
//......
}
If you want to manipulate the session in this method, you have to set the EnableSession property of WebMethod to True. That

[WebMethod (EnableSession = True)]//or [WebMethod (true)]
public static string GetUserName ()
{
//......
}
Then we write AJAX programs to access this program, we use jquery bar.

$.ajax ({
Type: "POST",
ContentType: "Application/json",
URL: "Webform2.aspx/getusername",
Data: "{}",
DataType: "JSON",
Success:function () {...}
});
Here are a few parameters to do a simple explanation,
Type: requested, post must be used here. The WebMethod method accepts only post-type requests.
ContentType: The content encoding type when sending information to the server. We must use Application/json here.
URL: The path to the server-side handler for the request, in the format "file name (with suffix)/method name"
Data: Parameter list. Note that the parameter here must be a JSON-formatted string, remembering the string format, such as: "{aa:11,bb:22,cc:33, ...}". If you write not a string, then jquery will serialize it into a string, then the server-side will not accept the JSON format, and can not be empty, even if there are no parameters to write "{}", as the above example.
Many people are not successful, the reason is here.
DataType: The type of data returned by the server. Must be JSON, others are invalid. Because WebService is a JSON format that returns data in the form of: {"D": "..."}.
Success: The callback function after the request succeeds. You can handle the returned data in any case here.

We can see that some of these parameter values are fixed, so from the perspective of reusability, we can do an extension to jquery and do a simple encapsulation of the above function:
We built a script file called Jquery.extend.js. Write a method called Ajaxwebservice in it (because WebMethod is actually webservice, so the method is valid for the request *.asmx), the code is as follows:
///
jquery prototype extension, reseal Ajax request Webserveice
///
///
The address of the processing request
///
///
Arguments, JSON-formatted strings
///
///
callback function after the request succeeds
///
$.ajaxwebservice = function (URL, dataMap, fnsuccess) {
$.ajax ({
Type: "POST",
ContentType: "Application/json",
Url:url,
Data:datamap,
DataType: "JSON",
Success:fnsuccess
});
}
OK, so we can ask the WebMethod method to write this:

$.ajaxwebservice ("Webform2.aspx/getusername", "{}", function (Result) {...});
Finally: If your project has a lot of Ajax programs (this is possible, I have done a website, is a SNS, full Ajax, almost every operation with Ajax),
You think it's very fragmented to write the WebMethod method on each ASPX page, so you can create a page (for example: webmethods.aspx) to store it.

Asp. NET using WebMethod

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.