1

I'm starting to convert a legacy .NET Framework ASP.NET API (which does not use the WebApi framework) into a .NET Core WebApi project. Due to dependencies, as an intermediate step I'm just swapping out the current API framework for WebApi, keeping the project targetting the .NET Framework.

The current version of the API supports binding comma-separated values on the query string into array properties on the model; I want to continue to support this.

So I need some custom model binding. There are plenty of similar questions about this, but they all seem to use .NET Core, different versions of the WebApi framework, and omit the namespaces being used (e.g. ASP.NET Core 1 Web API Model Binding Array).

I'm using .NET Framework 4.7 and version 5.2.7 of various Microsoft.AspNet.WebApi packages. It seems like I need a class that implements IModelBinder, but this interface exists in at least 3 different namespaces.

Exactly what IModelBinder interface do I need to implement, and once I've implemented it, how do I tell the WebApi framework to use my binder for specific properties?

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
  • You have an HTTP connection with a request and a response.The link is for a request which has header and an optional body.PUT is the optional data that is put into the body. The body is bytes.When you bind you are serializing your model and sending as bytes in the body of the request. So your application is a client and the Web API is the Server. So the server will deserialize and also need a copy of the model. The Net library provides libraries to automatically do the serialization or you can write your own code. the link your provided is a GET which is used on the server to deserialize. – jdweng Mar 09 '21 at 11:28
  • @jdweng I don't think this really addresses my question. This doesn't have anything to do with HTTP methods, there are no byte arrays, I just want to deserialize API requests that contain comma-separated values that I want to interpret as arrays. There are plenty of examples for .NET Core, but I just want to know the precise interface(s) to implement and how to use them. – Graham Clark Mar 09 '21 at 13:15
  • How do you think data gets transmitted between a client and server? – jdweng Mar 09 '21 at 13:32
  • @jdweng I'm quite clear on how data is transmitted between a client and a server thanks. Maybe I've been unclear in my question, but it's specifically on what interface in what namespace to implement for some custom model binding. The question I linked to does not mention PUTs. The WebApi framework provides some default binding to turn an HTTP request into a .NET object used as a parameter in a controller method. I just want to augment this default binding to handle arrays so as to not break the current API contract. – Graham Clark Mar 09 '21 at 14:02
  • See the OnPost method : https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0#bind-attribute. the data is being sent in the Body of the http so you need to convert to a base64 string. HTML doesn't allow special characters. Or the data needs to be a mime attachment. – jdweng Mar 09 '21 at 14:39

1 Answers1

1

Since you are targeting Web API that is implemented via Microsoft.AspNet.WebApi packages, you need to use types from these packages. Specifically, you need IModelBinder from the namespace System.Web.Http.ModelBinding (it is defined in System.Web.Http.dll provided by Microsoft.AspNet.WebApi.Core package).

Please refer to THIS on how to implement and use binding comma-separated values on the query string.

Ruslan Gilmutdinov
  • 1,217
  • 2
  • 9
  • 20
  • awesome thank you, I hadn't seen that question. Just need to get this working for properties of complex types, perhaps I will end up needing a `TypeConverter`, however the question you've linked to covers that somewhat as well. – Graham Clark Mar 09 '21 at 16:43