Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Media Resource Support for OData POST in Web API

I would like to create oData controller to upload files

FileDto

  1. FileId
  2. NameWithExtension (Type: String)
  3. Metadata (Type: List)
  4. Content (Type: Stream)

=========================Http Request Actions==================

• GET: ~/Files({id})

Content-Type: application/json
Result: FileDto without Content

• GET: ~/Files({id})

Content-Type: application/octet-stream
Result: Stream of the File only

• POST: ~/Files

Content-Type: ?
Body: FileDto with Content
Result: FileId

Not sure how i can achieve this when coupled with OData.

Thanks in advance

like image 594
Navap Avatar asked May 03 '14 06:05

Navap


People also ask

How do I enable OData in Web API?

In Solution Explorer, right click on Models folder > Add > Class > Name your class. Now, we are going to create a Controller. Right click on the Controllers folder > Add > Controller> selecting Web API 2 OData v3 Controller with actions, using Entity Framework > click Add.

What is OData in Web API with example?

The Open Data Protocol (OData) is a data access protocol for the web. OData provides a uniform way to query and manipulate data sets through CRUD operations (create, read, update, and delete). ASP.NET Web API supports both v3 and v4 of the protocol.

What method does a Web API use to ensure that a request is valid?

A request that might take a long time to process should be performed without blocking the client that submitted the request. The web API can perform some initial checking to validate the request, initiate a separate task to perform the work, and then return a response message with HTTP code 202 (Accepted).

What is Odataqueryoptions?

OData defines parameters that can be used to modify an OData query. The client sends these parameters in the query string of the request URI. For example, to sort the results, a client uses the $orderby parameter: http://localhost/Products?$orderby=Name. The OData specification calls these parameters query options.


1 Answers

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.

like image 59
Ricardo Valente Avatar answered Nov 06 '22 11:11

Ricardo Valente