我有一个名为购买的模型,具有多种属性:
public class Purchase
{
[JsonProperty(PropertyName = "amount", NullValueHandling = NullValueHandling.Ignore)]
public int Amount
{
get;
set;
}
[JsonProperty(PropertyName = "currency_code", NullValueHandling = NullValueHandling.Ignore)]
public string CurrencyCode
{
get;
set;
}
}
JSON是:
{
amount: 3000,
currency_code: "USD"
}
控制器为:
[AllowAnonymous]
public class ProtoController : Controller
{
private readonly IPurchaseManagerFactorySelector purchaseManagerFactorySelector = null;
public ProtoController(IPurchaseManagerFactorySelector purchaseManagerFactorySelector)
{
this.purchaseManagerFactorySelector = purchaseManagerFactorySelector;
}
[Route("opt/proto/index/{identifier}")]
[HttpGet]
public async Task<ActionResult> Index(string identifier)
{
return View();
}
[Route("opt/proto/index/{identifier}")]
[HttpPost]
public async Task<ActionResult> Index(string identifier, Request.Purchase purchase)
{
IPurchaseManager purchaseManager = purchaseManagerFactorySelector.GetFactory(identifier);
return View();
}
}
将JSON发布到ApiController时,所有属性均正确填充,但是,将同一JSON发布到Controller时,仅填充金额-CurrencyCode为null。我简要地添加了一个名为Currency_Code的新属性,并填充了它,看来JsonProperty属性在MVC下被忽略了吗?我添加了DataMember属性,但是它没有任何作用,因此我将其删除。正在使用Json.Net,但是我不知道有什么区别?
答案 0 :(得分:0)
我将从参数中删除Request.Purchase,然后尝试使用[FromBody]属性进行参数绑定。
如果您要更改的只是从Controller到ApiController的Controller继承,我不确定,但这听起来像您的消息标题。
如果您在Chrome浏览器中拨打电话并使用检查工具,则应该能够检查所拨打的每个电话中的标题并查看正在填充的内容