How to implement the method in Json using Serializable?

6

  • GET
  • PUT
  • DELETE
  • POST

I made a GET method, looked like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    DataClassesDataContext dc = new DataClassesDataContext();

    [WebMethod]
    public string getUsuario(string id)
    {
        var json = "";
        var usuario = from result in dc.TB_USUARIOs
                      where result.controleusuario == Int32.Parse(id)
                      select result;
        JavaScriptSerializer jss = new JavaScriptSerializer();
        json =  jss.Serialize(usuario);
        return json;
    }
}
    
asked by anonymous 18.05.2015 / 22:30

3 answers

1

ASMX Webservices only support POST and GET . ASMX is a legacy technology and should not be considered in new developments , mainly because Microsoft since 2009 has announced no further improvements in this technology.

I suggest using Web.Api , which is the latest .Net technology for REST service creation and supports all the HTTP methods you need. Or even the WCF - In this article there is a good example of how to use the different HTTP methods with WCF.

Still, here's a tip if you want to use POST with ASMX: just add the ScriptMethod with parameter UseHttpGet with value false , like this:

[ScriptMethod(UseHttpGet = false)]
public string UmMetodo()
{
    return "Olá Mundo!";
}
    
19.05.2015 / 01:41
0

As already said by Marcus Vinícius, to implement HttpVerbs the ideal is to use the Web API.

Just for documentation, below I've put some links explaining how to do this in WCF. See that it is much more complex to do it because WCF is much more than a simple platform for implementation of Web Services or REST Services.

A Beginner's Tutorial on Creating WCF REST Services
22.06.2015 / 15:32
0

As already seen from the other answers, it is not recommended to use WebServices , but this does not mean that it is not possible.

I decorated your method getUsuario so that it returns a valid JSON, using POST :

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public string getUsuario(string id)
{
    var json = "";
    var usuario = from result in dc.TB_USUARIOs
                  where result.controleusuario == Int32.Parse(id)
                  select result;

    JavaScriptSerializer jss = new JavaScriptSerializer();
    json = jss.Serialize(usuario);

    Context.Response.Clear();
    Context.Response.ContentType = "application/json";
    Context.Response.Flush();
    Context.Response.Write(json);
}
    
22.06.2015 / 17:03