简体   繁体   中英

URL to call the JSON WebService produced into ASP.NET

I have done a web service using Visual Studio 2012 into C#. I'm usually use SOAP but now I need to use JSON. So I've used this code to create a SOAP method:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string test() 
{
     var testArray = new List<clienti>();
     testArray.Add(new clienti() {nome = "Mark", cognome = "Reed", email = "mark.reed@test.com"});
     var serializer = new JavaScriptSerializer();
      return serializer.Serialize(testArray);
}

Can I call this method directly from an URL? (that is the real goal that I want to obtain) A stuff like this: http://ws.mysite.com/serice1.asmx/test .

If I try to type the URL with the name of the method after the slash not return data but an error on the URL format!!! In debug mode all works perfectly if I click the button method 在此处输入图片说明

So the answer to your question is Yes I believe, but let's start for sure with an Ajax scenario that 100% works.

Something I want to clarify for you though, your "test()" method in service1.asmx is not a "Soap method" as you called it. Its a "Web Service method" and it returns JSON. It is callable using HTTP protocol or SOAP protocol. Because you added the [ScriptMethod] to the Web Method you can now call it using an HTTP Get request. If you remove the [ScriptMethod] attribute, you can only call "test()" with an HTTP POST request or a SOAP request.

The following jQuery Ajax call will work with service1.asmx/test:

 $.ajax({
            type: "POST",
            url: "Service1.asmx/test",
            data: "{ ifTestHadAParameter: 'hello world'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var myData = JSON.parse(data.d); // data.d is a JSON formatted string, to turn it into a JSON object
                                                 // we use JSON.parse
                // now that myData is a JSON object we can access its properties like normal
            }
        });

If you are using ASP.NET 3.5 or below, ASP.NET Web Services wrap JSON responses to HTTP Get and Post requests in an outer JSON {} to project against a certain type of hacking. Hence the data.d in my jQuery Ajax code snippet. If you using ASP.NET > 4.0 then, in your success function data contains your response from service1.asmx/test, no data.d required.

Now to call http://ws.mysite.com/serice1.asmx/test , this will not work in a "browser" as you say but you probably ultimately do not want to use the URL in a browser, that is just how you stated your question for simplicity.

You need to format your Get request correctly with http://ws.mysite.com/serice1.asmx/test. . So in C#, using the HTTPRequest .NET lib you should be able to prepare a proper HTTP Get Request to call your Web Services URL, basically simulating a browser request in your C# code if you have the right contentType in your Get request header.

A great tool to use for this problem is called Fiddler , you can easily create HTTP Get requests and change the content type.

No as asmx webservices have no router's like webapi or MVC best create a webapi project, add a controller with empty read write methods and return your array in there it will serialize automatically in JSON,XML or BSON default JSON (you can change this in the accept header in you client request) example method

public class mycontroller: ApiController
  {

    public string Get()
    {
      var testArray = new List<clienti>();
      testArray.Add(new clienti() {nome = "Mark", cognome = "Reed", email =    "mark.reed@test.com"});
       return testArray ;
    }

you can access the url like this site/api/controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM