我刚刚开始使用Web Api,在完成一些教程和小型测试项目之后,我有足够的信心建立一个真实的世界"应用。
在我第一次创建解决方案时,我删除了我认为不必要的所有内容。在这个练习中,我必须删除一些绑定器,因为当我POST / PUT一个复杂的对象时,它总是为空。
简而言之,我的问题是在web api中,什么是默认的模型绑定器?我在哪里引用它们?下面我已经介绍了我想要做的以及我尝试解决此问题的内容更详细。
我想做什么 我有两个解决方案在运行,在每个解决方案中我都有相同的类。我试图将一个复杂的对象从解决方案A传递到解决方案B.当我点击解决方案B中的断点时,传递的值始终为空(解决方案B是我的"现实世界"项目有问题)。
我尝试了什么 在阅读了几个stackoverflow线程和how web api does binding之后,我尝试在同一个解决方案中调用一个动作并传递相同的对象,IT工作了!例如
我已经比较了解决方案,试图找出可能导致此问题的我已删除的内容,但没有任何成功。我可以创建一个新的解决方案来解决问题,但我不觉得我会从这条路线中学到任何东西。如果有人能指出我正确的方向,我会非常感激,所以我可以解决这个问题并学习更多关于web api的文章。
可在同一解决方案中运行的代码
解决方案A
public const string uri = "http://localhost:49611/api/Values";
public async Task<string> PutAValue()
{
var propertyForm = new AddUpdatePropertyForm();
var network = new Network { NetworkID = 123456 };
var branch = new Branch { BranchID = 1, Channel = Channel.Sales, Overseas = false };
var property = new Property { AgentRef = "123", Published = true, PropertyType = PropertyType.SemiDetachedBungalow, Status = TransactionStatus.Available };
propertyForm.Network = network;
propertyForm.Branch = branch;
propertyForm.Property = property;
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var responce = await httpClient.PutAsJsonAsync<AddUpdatePropertyForm>(uri, propertyForm);
...more stuff...
}
}
解决方案A
public void Put([FromBody]AddUpdatePropertyForm form)
{
...Has all passed values but IN THE SAME SOLUTION...
}
使用解决方案a和解决方案b无效的代码
解决方案A
public const string uri = "http://localhost:58814/api/sample";
public async Task<string> PutAValue()
{
var propertyForm = new AddUpdatePropertyForm();
var network = new Network { NetworkID = 123456 };
var branch = new Branch { BranchID = 1, Channel = Channel.Sales, Overseas = false };
var property = new Property { AgentRef = "123", Published = true, PropertyType = PropertyType.SemiDetachedBungalow, Status = TransactionStatus.Available };
propertyForm.Network = network;
propertyForm.Branch = branch;
propertyForm.Property = property;
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var responce = await httpClient.PutAsJsonAsync<AddUpdatePropertyForm>(uri, propertyForm);
...more stuff...
}
}
解决方案B
public async Task<HttpResponseMessage> Put([FromBody]AddUpdatePropertyForm propertyForm)
{
...propertyForm ALWAYS null...
}
使用解决方案a和解决方案b的代码(但我想接收对象而不是字符串 - 正如我在同一解决方案中传递它时能够做的那样)
解决方案A
public const string uri = "http://localhost:58814/api/sample";
public async Task<string> PutAValue()
{
var propertyForm = new AddUpdatePropertyForm();
var network = new Network { NetworkID = 123456 };
var branch = new Branch { BranchID = 1, Channel = Channel.Sales, Overseas = false };
var property = new Property { AgentRef = "123", Published = true, PropertyType = PropertyType.SemiDetachedBungalow, Status = TransactionStatus.Available };
propertyForm.Network = network;
propertyForm.Branch = branch;
propertyForm.Property = property;
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new JavaScriptSerializer().Serialize(propertyForm);
var response = await httpClient.PutAsJsonAsync<string>(uri, content);
...more stuff...
}
}
解决方案B
public async Task<HttpResponseMessage> Put([FromBody]string propertyForm)
{
var content = new JavaScriptSerializer().Deserialize<AddUpdatePropertyForm>(propertyForm);
}