我的webApi控制器
[HttpPost]
public ISearchProviderCommandResult ExecuteCommand(ISearchProviderCommand command)
{
MySearchProvider searchProvider = new MySearchProvider();
return searchProvider.ExecuteCommand(command);
}
我的searchCommand对象
[Serializable]
class SearchProviderCommand : ISearchProviderCommand
{
public string OperationContext {get; set;}
public string OperationId{get; set;}
}
我的webapi控制器中有一个断点,并且该线路上有一个断点 HttpWebResponse response =(HttpWebResponse)request.GetResponse();
当我尝试进入webapi控制器时,我收到500错误,它甚至没有击中webapi控制器内的断点。以下是我的问题:
编辑: 基于asp.net论坛和leons方向,我改变了我的WebApi控制器以使用对象而不是接口,它可以工作:
[HttpPost]
public SearchProviderCommandResult ExecuteCommand(SearchProviderCommand command)
{
MySearchProvider searchProvider = new MySearchProvider();
return searchProvider.ExecuteCommand(command);
}
你能告诉我如何从结果中重建我的对象吗?
编辑:基于leon的建议 - 对于那些感兴趣的人是我的最终来电代码
public result ExecuteCommand(ISearchProviderCommand searchCommand)
{
//serialize the object before sending it in
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonInput = serializer.Serialize(searchCommand);
HttpClient httpClient = new HttpClient() { BaseAddress = new Uri(ServiceUrl) };
StringContent content = new StringContent(jsonInput, Encoding.UTF8, "application/json");
var output = httpClient.PostAsync(ServiceUrl, content).Result;
//deserialize the output of the webapi call
result c = serializer.Deserialize<result>(output.Content.ReadAsStringAsync().Result);
return c;
}
}
public class result : ISearchProviderCommandResult
{
public object Result { get; set; }
}
答案 0 :(得分:1)
你正在使用BinaryFormatter?您不应该将您的请求编码为JSON,因为这就是您要发送的内容吗?