我正在尝试通过使用 HTTPContent 的 Post 方法将具有具有列表的属性的类传递给 Restful 服务。这是我的控制器代码:
protected async Task<T> GetOfflineReport<T>(ReportSettings rSettings)
{
string postUrl = "http://localhost/Application/ReportingDataService/v1/GetOfflineReport2012";
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
PlatformClient _platform = new PlatformClient();
T result = default(T);
using (HttpContent httpContent = new StringContent(jsSerializer.Serialize(rSettings)))
{
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using (HttpResponseMessage response = await _platform._HttpClient(Session).PostAsync(postUrl, httpContent))
{
if (response.IsSuccessStatusCode)
result = await response.Content.ReadAsAsync<T>();
}
}
return (result);
}
在上述控制器方法中,“ReportSettings”类如下:
[DataContract]
public class ReportSettings
{
[DataMember]
public string ReportName
{
get;
set;
}
[DataMember]
public string ReportPath
{
get;
set;
}
[DataMember]
public ReportFormat ReportFormat
{
get;
set;
}
[DataMember]
public List<ReportParams> Parameters
{
get;
set;
}
}
当我将“reportSettings”参数作为 null 传递时,它将使用服务方法。但是,当“reportSettings”与参数列表一起传递时,它没有命中服务方法。
服务方式如下:
[WebInvoke(Method = "POST", UriTemplate = "GetOfflineReport2012")]
[OperationContract]
IEnumerable<byte> GetOfflineReport2012(ReportSettings reportSettings);
public IEnumerable<byte> GetOfflineReport2012(ReportSettings reportSettings)
{
try
{
var dp = _mDbFactory.GetReportingDataProvider();
var result = dp.GetOfflineReport2012(reportSettings);
return result;
}
catch (Exception ex)
{
throw _convertToWebFaultException(ex);
}
}
你能帮我解决这个问题吗?如何将报告设置的值传递给服务。我没有收到任何异常,但它没有影响服务。