复杂的参数调用.NET Web服务 [英] Calling .net web service with complex parameters
问题描述
我已经看到了与WCF在asp.net接受之一参数JSON Web服务的例子很多。我需要我的服务的方法来接受许多参数和返回像这样的列表;
I have seen many examples of json web services with WCF in asp.net that accept one in-parameter. I need my service method to accept many parameters and return a list like this;
这是我的Service.svc:
This is my Service.svc:
public class Car
{
public string Title;
public int Number;
}
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public List<Car> GetCarById(int userid, string password , List<Cars> carlist)
{
//Doing some stuff here
}
}
在我的web.config:
In my web.config:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="Service">
<endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="Service" />
</service>
</services>
</system.serviceModel>
如何,如果我想通过这些参数,我把这个服务?当我尝试打电话与PARAMS用户ID,密码和卡洛斯的方法,他们都是空。这是我送 HTTP://localhost/Service.svc/GetCarById 称为应用程序中使用POST菲德勒:
How can I call this service if I want to pass all these parameters? When I try to call the method with the params userid, password and carlist, they are all null. This is what I send to http://localhost/Service.svc/GetCarById using POST in an application called Fiddler:
HEADERS:
Host: localhost
Content-Length: 136
Content-Type: application/json; charset=utf-8
BODY:
{"d":{"password":"test","userid":123,"carlist":[{"__type":"Car","Number":1,"Title":"BMW"},{"__type":"Car","Number":2,"Title":"Honda"}]}}
响应为:{D:空}该方法返回卡洛斯,所以我知道调用该方法,而且卡洛斯为空。如果我改变了方法返回例如错误的字符串我找回来,所以我知道该方法被调用。
The response is: {"d":null} The method is returning carlist, so I know the method is called and that carlist is null. If I change the method to return a string for example "Error" I get that back, so I know the method is called..
你有一个想法是什么,我做错了?
Do you have an idea what I'm doing wrong?
推荐答案
终于找到了解决办法:
从更改:
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
要刚:
[WebInvoke]
然后我叫不最初的D的服务,一切都运行完美!
And then I called the service without the initial "d", everything works perfect!
感谢您的帮助!
这篇关于复杂的参数调用.NET Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!