An analysis of the overloads of the. Net WebService method, support sessions, support request requests, and response formats

Source: Internet
Author: User

. NET WebService does not support overloading of Web methods, but you can achieve the purpose of overloading Web methods by setting the MessageName field of the WebMethod property.

By setting the enablesession=true of the WebMethod property, you can have webservice support the session.

By setting the Usehttpget=false of the Scriptmethod property, you can have the Web method accept only the POST request, or, if usehttpget=true, allow the Web method to accept the GET request.

You can format the Web method response by setting the Responseformat of the Scriptmethod property. such as: Responseformat = Responseformat.json.

On the code description:

WebService code for. NET:

[WebMethod (Description ="Verifying logon Actions", enablesession =true, MessageName ="Login")] [Scriptmethod (Usehttpget=false, Responseformat =Responseformat.json)] PublicResult Login (stringUserName,stringpwd) {Result RC=NULL; Try {            if(UserName = ="1"&& pwd = ="1") {session["User"] =NewUserInfo {UserName = UserName, pwd =pwd}; RC= Result.toresult ("true","Login Successful"); }            Elserc = Result.toresult ("false","Logon Failure"); }        Catch(Exception ex) {//You can save the log hererc = Result.toresult ("false", ex.        Message); }        returnRC; } [WebMethod (Description="Verify Login", enablesession =true, MessageName ="IsLogin")] [Scriptmethod (Usehttpget=false, Responseformat =Responseformat.json)] Publicresult IsLogin () {result RC=NULL; Try{RC= session["User"] !=NULL? Result.toresult ("true","already logged in"): Result.toresult ("false","not logged in"); }        Catch(Exception ex) {RC= Result.toresult ("false", ex.        Message); }        returnRC; }     Public classResult { Public stringCode {Get;Set; }  Public stringMessage {Get;Set; }  Public StaticResult Toresult (stringCodestringmessage) {            return NewResult {code = code, Message =message}; }    }

HTML code under the same site:

<! DOCTYPE Html>"Content-type"Content="text/html; Charset=utf-8"/> <title></title> <meta charset="Utf-8"/> <script src="Http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script>function Login () {varURL ="Http://localhost/wst/Service.asmx/Login"; $.ajax ({url:url, type:'POST', Data:'{"UserName": "1", "pwd": "1"}',//corresponds to parameter one by one of the WebService Web methodDataType:'JSON', Cache:false, ContentType:'Application/json',//                   error:function () {}, Success:function (data) {if(DATA.D! =NULL) {alert (Data.d.code+"  "+data.d.message); }                    ElseAlert"request failed! ");        }            }); }    </script>"Button"Value="Login"onclick="Login ()"/></body>

Use jquery ajax to access the homologous webserver method.

An analysis of the overloads of the. Net WebService method, support sessions, support request requests, and response formats

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.