简体   繁体   English

调用ASP.NET中生成的JSON WebService的URL

[英]URL to call the JSON WebService produced into ASP.NET

I have done a web service using Visual Studio 2012 into C#. 我已经使用Visual Studio 2012到C#中完成了一个Web服务。 I'm usually use SOAP but now I need to use JSON. 我通常使用SOAP,但现在需要使用JSON。 So I've used this code to create a SOAP method: 因此,我已使用以下代码创建SOAP方法:

[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? 我可以直接从URL调用此方法吗? (that is the real goal that I want to obtain) A stuff like this: http://ws.mysite.com/serice1.asmx/test . (这是我想要获得的真正目标)这样的东西: 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!!! 如果我尝试在斜杠后输入带有方法名称的URL,则不会返回数据,而是URL格式错误!!! 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. 因此,您的问题的答案是肯定的,但是让我们从100%有效的Ajax场景开始。

Something I want to clarify for you though, your "test()" method in service1.asmx is not a "Soap method" as you called it. 不过,我想为您澄清一下,service1.asmx中的“ test()”方法不是您所谓的“ Soap方法”。 Its a "Web Service method" and it returns JSON. 它是“ Web服务方法”,它返回JSON。 It is callable using HTTP protocol or SOAP protocol. 可以使用HTTP协议或SOAP协议进行调用。 Because you added the [ScriptMethod] to the Web Method you can now call it using an HTTP Get request. 因为您已将[ScriptMethod]添加到Web方法中,所以现在可以使用HTTP Get请求对其进行调用。 If you remove the [ScriptMethod] attribute, you can only call "test()" with an HTTP POST request or a SOAP request. 如果删除[ScriptMethod]属性,则只能使用HTTP POST请求或SOAP请求调用“ test()”。

The following jQuery Ajax call will work with service1.asmx/test: 以下jQuery Ajax调用将与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. 如果您使用的是ASP.NET 3.5或更低版本,则ASP.NET Web服务会将JSON响应包装在外部JSON {}中,以用于HTTP Get和Post请求,以针对某种类型的黑客行为进行投影。 Hence the data.d in my jQuery Ajax code snippet. 因此,我的jQuery Ajax代码片段中的data.d。 If you using ASP.NET > 4.0 then, in your success function data contains your response from service1.asmx/test, no data.d required. 如果使用ASP.NET> 4.0,则成功函数中的数据将包含来自service1.asmx / test的响应,而无需data.d。

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. 现在调用http://ws.mysite.com/serice1.asmx/test ,这在您所说的“浏览器”中将不起作用,但是您最终可能不想在浏览器中使用该URL,这就是方法您为简单起见说了您的问题。

You need to format your Get request correctly with http://ws.mysite.com/serice1.asmx/test. 您需要使用http://ws.mysite.com/serice1.asmx/test.正确设置Get请求的格式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. 因此,在C#中,使用HTTPRequest .NET库,您应该能够准备适当的HTTP Get Request来调用Web服务URL,如果您在Get request标头中具有正确的contentType,则基本上可以在C#代码中模拟浏览器请求。

A great tool to use for this problem is called Fiddler , you can easily create HTTP Get requests and change the content type. 解决此问题的一个好工具叫做Fiddler ,您可以轻松地创建HTTP Get请求并更改内容类型。

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 不能,因为asmx web服务没有像webapi或MVC这样的路由器,最好创建一个webapi项目,添加具有空读写方法的控制器,并在其中返回数组,它将自动以JSON,XML或BSON默认JSON序列化(您可以在在客户端请求中接受标头)示例方法

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 您可以像此site / api / controller一样访问网址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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