How to implement methods in Json using Serializable?

Asked

Viewed 130 times

6

I’m making a webservice where I want to return the data in JSON, how I could implement the methods, which is the best way?

  • GET
  • PUT
  • DELETE
  • POST

I did a GET method, it went 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;
    }
}
  • None of the answers answers your question?

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 that it will no longer make improvements to this technology.

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

Still, here’s a hint if you want to use POST with ASMX: just add the attribute Scriptmethod with the parameter UseHttpGet worthwhile false, thus:

[ScriptMethod(UseHttpGet = false)]
public string UmMetodo()
{
    return "Olá Mundo!";
}

0

  • Luiz, welcome to Sopt. Links may be offline in the near future (which would invalidate your response). I suggest that edit your response and provide content to the links.

0

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

I memorized 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);
}
  • None of the answers answers your question?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.