1

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[]'."

enter image description here

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/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions

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):

Pass two byte array to asp.net web api post method

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.