C# 如何在restful服务方法中将类对象作为参数传递?
我想将对象作为参数传递到restful服务方法中。下面是接口代码C# 如何在restful服务方法中将类对象作为参数传递?,c#,.net,wcf,rest,C#,.net,Wcf,Rest,我想将对象作为参数传递到restful服务方法中。下面是接口代码 [OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "searchCMSExposure/{requestObject}")] ResponseObject<GenericCollection<CONT_EXPRInfo>>
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,
UriTemplate = "searchCMSExposure/{requestObject}")]
ResponseObject<GenericCollection<CONT_EXPRInfo>> SearchCMSExposure(RequestObject<ProposalInfoParm> requestObject);
[运营合同]
[WebGet(ResponseFormat=WebMessageFormat.Json,
UriTemplate=“searchCMSExposure/{requestObject}”)]
ResponseObject SearchCMSExposure(请求对象请求对象);
正如预期的那样,我在协定“IProposalService”中得到了错误操作“SearchCMSExposure”,该操作有一个名为“requestObject”的路径变量,该变量没有“string”类型。UriTemplate路径段的变量必须具有“string”类型。因为它需要字符串类型。如何在restful服务方法中将对象作为参数传递?请尝试以下代码
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
public class StackOverflow_6783264
{
public class InputData
{
public string FirstName;
public string LastName;
}
[ServiceContract]
public interface ITest
{
[OperationContract]
[WebGet(UriTemplate = "/InsertData?param1={param1}")]
string saveDataGet(InputData param1);
[OperationContract]
[WebInvoke(UriTemplate = "/InsertData")]
string saveDataPost(InputData param1);
}
public class Service : ITest
{
public string saveDataGet(InputData param1)
{
return "Via GET: " + param1.FirstName + " " + param1.LastName;
}
public string saveDataPost(InputData param1)
{
return "Via POST: " + param1.FirstName + " " + param1.LastName;
}
}
public class MyQueryStringConverter : QueryStringConverter
{
public override bool CanConvert(Type type)
{
return (type == typeof(InputData)) || base.CanConvert(type);
}
public override object ConvertStringToValue(string parameter, Type parameterType)
{
if (parameterType == typeof(InputData))
{
string[] parts = parameter.Split(',');
return new InputData { FirstName = parts[0], LastName = parts[1] };
}
else
{
return base.ConvertStringToValue(parameter, parameterType);
}
}
}
public class MyWebHttpBehavior : WebHttpBehavior
{
protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
{
return new MyQueryStringConverter();
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "").Behaviors.Add(new MyWebHttpBehavior());
host.Open();
Console.WriteLine("Host opened");
WebClient client = new WebClient();
Console.WriteLine(client.DownloadString(baseAddress + "/InsertData?param1=John,Doe"));
client = new WebClient();
client.Headers[HttpRequestHeader.ContentType] = "application/json";
Console.WriteLine(client.UploadString(baseAddress + "/InsertData", "{\"FirstName\":\"John\",\"LastName\":\"Doe\"}"));
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}