Media Resource Support for OData POST in Web API

前端 未结 1 1031
眼角桃花
眼角桃花 2021-02-19 12:49

I would like to create oData controller to upload files

FileDto

  1. FileId
  2. NameWithExtension (Type: String)
  3. Metadata (Type: List)
  4. Co
相关标签:
1条回答
  • 2021-02-19 13:32

    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.

    0 讨论(0)
提交回复
热议问题