0

I am trying to get a controller to take json data from a post request and set how I wish them to be applied in the model. Example:

public string model1 {
    public string name { get; set; }
    public string value { get; set; }
}

public string model2 {
    public string ident { get; set; }
    public IEnumerable<model1> model { get; set; }
}

I want model1 to contain the key, value of the properties in the json object. For example { ident: "moo", model: { fish: "sticks" } } would produce model2 with ident: moo and ref model1 with an single entry containing name: fish and value: sticks.

I could easily do it in the controller, I am kind of wondering if there was a way via web api 2 that would allow me to specify how I wanted that model handled so I did not have to replicate it in every controller when a model ref model1.

Thanks

  • 1
    You can create a [custom model binder](https://learn.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding?view=aspnetcore-5.0) – Liam Oct 01 '21 at 12:22
  • I tried model binding. It seems to only work if the model is on the root and not a model in a model. :( – David Hubner Oct 01 '21 at 14:29
  • Yes you'd need to bind `model2`. There is only one "model" not two here and that is `model2`. `model1` is just an object contained in model2. The solution is still the same – Liam Oct 01 '21 at 14:32
  • Why are you using Web API 2.0? That was [released almost 10 years ago](https://stackoverflow.com/a/51391202/159145) and was quickly supplanted by later versions. It's quick and painless to update, what's the hold-up? – Dai Nov 27 '21 at 20:59
  • @Dai: is just a tag. WebApi2 was named to distinguish it from 1. Higher versions are often still referred to as "2". – Wiktor Zychla Nov 27 '21 at 21:18
  • @WiktorZychla No. I guarantee that absolutely no-one refers to ASP.NET Web API 3.0 as "webapi2". – Dai Nov 27 '21 at 21:34
  • @Dai: by webapi 3 people usually refer to webapi introduced with net core. The net framework webapi is still referred to as webapi2 even though it's 5.X. if by chance you refer to .net core, it's not a matter of "updating". On the other hand, if you mean .net framework, I bet OP uses webapi 5.X and they just label it 2 as everyone does. – Wiktor Zychla Nov 27 '21 at 22:04
  • @DavidHubner: if your JSON had just a single child and no indication of a possible set, it possibly won't bind to IEnumerable. This { ident: "moo", model: [ { fish: "sticks" } ] } would better fit your model. Is this your primary concern? Or you want dynamic key names in the child? – Wiktor Zychla Nov 27 '21 at 22:09

1 Answers1

-1

You may create a custom FilterAttribute and apply that new custom filter attribute globally within WebAPIConfig.cs file under Register Method

public static void Register(HttpConfiguration config)
{
    config.Filters.Add(new YourCustomFilterAttribute());
    ......
    ......
    ......
}

This custom filter attribute will run before every API-Method call. hopefuly it will serve the core purpose.