I am currently building an ASP.NET MVC WebAPI service for which I'd like a small piece of binary data to be sent in the querystring. The method in this application is invoked through POST and the body contains the actual data. The querystring is used to describe some properties of the data in the body (e.g. how it is encoded).
I wonder if it is possible to accept a byte array parameter as base64 encoded string in the query string.
As an example I have this code:
using System.Text;
using System.Web.Http;
namespace WebApplication2.Controllers
{
public class ByteArrayController : ApiController
{
public string Get([FromUri] byte[] myarray)
{
var strbResult = new StringBuilder();
foreach (byte byteValue in myarray)
{
strbResult.AppendLine(byteValue.ToString() + " ");
}
return strbResult.ToString();
}
}
}
I then want to be able to send the parameter myarray in the following way:
http://.../api/ByteArray/Get?myarray=MTIzNA%3D%3D
Please don't mind the naming, it's just a simple example.
Now I know that arrays can be sent by using the same parameter multiple times in the querystring (e.g. myarray=1&myarray=2&myarray=3) but I'd like to accept it as a base64 encoded string.
I have been looking for an attribute to specify how to decode the array but wasn't able to find such attribute.
Of course I can modify the code to accept a string and then convert it to a byte array but my preference is to do this transparantly, if possible.