I've got one method, which take a model [AccountLinkRequest] as a parameter with url-encoded data. It's uses Json.NET by default, and also, I can't use the setting UseDataContractJsonSerializer = true cause I have generic output response model (in other methods)

[HttpPost]
public SomeResponse Link(AccountLinkRequest request)
{
    if (request.CustomerId == null)
        throw new Exception("Deserialization error here, pls help!");

    // other actions
}

Here is my model class:

[DataContract]
[JsonObject]
public class AlertAccountLinkRequest
{
    [DataMember(Name = "id")]
    public string id { get; set; }

    [DataMember(Name = "customer_id")]
    [JsonProperty("customer_id")]
    public string CustomerId { get; set; }
}

The problem: request.CustomerId is allways null. The request is pretty simple:

web_service_URL/link?customer_id=customer_id&id=id (url-encoded)

if I use Customer_Id instead of CustomerId, everything will be fine, but I'm on a jedy-way. Thank you!

有帮助吗?

解决方案

There is not a simple answer how to achieve that. For more details please read this:

How to bind to custom objects in action signatures in MVC/WebAPI

Summary:

  1. Manually call the parse function inside of your Action
  2. Use a TypeConverter to make the complex type be simple
  3. Use a custom model binder

So, if you for instance create your 'SmartBinder' which is able to consume some attributes, you can get what you want. Out fo the box there is no functionality for that, just the naming conventions...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top