I am using OData V4 with ASP.NET Web API. After doing a lot of research and experiments I am still not able to pass the Byte array (byte[]) as a class model property. I am not sure that how the .Net Framework is receiving it as a string. But when I configure the controller action to receive only the Byte array (byte[]) parameter then it receives the correct data. It does not receive it as a class property.
Here is my class model.
public class TempFileModel
{
public string Name { get; set; }
public byte[] Content { get; set; }
}
Here is the OData model configuration:
public class TransientFileConfiguration : IModelConfiguration
{
public void Apply(ODataModelBuilder builder, ApiVersion apiVersion, string routePrefix)
{
builder.EntitySet<TransientFileModel>("TransientFiles");
builder.EntityType<TransientFileModel>().HasKey(e => e.Key);
builder.Action("UploadFile")
.Returns<List<string>>()
.CollectionParameter<TempFileModel>("file");
}
}
My Controller action for this:
[HttpPost]
[ODataRoute("UploadFile")]
public async Task<IHttpActionResult> UploadFile(ODataActionParameters parameters)
{
var paramValue = parameters["file"];
return Ok(new List<string>());
}
The error I get every time: Error Message: "Invalid cast from 'System.String' to 'System.Byte[]'."
Here are few links that I have searched for unbound action using OData API and working with this issue:
https://learn.microsoft.com/en-us/odata/webapi/action-parameter-support
How to pass Array to OData function in ASP.NET Web API implementation?
How to make an unbound POST action in webapi 2.2 odata
A question on StackOverflow with similar context maybe (just to understand the issue):
