I would like to create oData controller to upload files
FileDto
This page explains how to create an oDataController.
1) To install the package on your project, open the console manager and type this:
Install-Package Microsoft.AspNet.Odata
2) Open your WebApiConfig.cs
and, inside Register
method, add this code:
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<FileDto>("File");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
3) Create your oDataController replacing the yourDataSourceHere
to use your own class:
public class FileController : ODataController
{
[EnableQuery]
public IQueryable<FileDto> Get()
{
return yourDataSourceHere.Get();
}
[EnableQuery]
public SingleResult<FileDto> Get([FromODataUri] int key)
{
IQueryable<FileDto> result = yourDataSourceHere.Get().Where(p => p.Id == key);
return SingleResult.Create(result);
}
public IHttpActionResult Post(FileDto File)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
yourDataSourceHere.Add(product);
return Created(File);
}
}
OBS: To test this solution, I changed the FileDto
's property Content
. More specifically, it's type! From Stream
to byte[]
. Posted the content as Base64 string.