媒体资源支持的OData POST在网页API [英] Media Resource Support for OData POST in Web API

查看:175
本文介绍了媒体资源支持的OData POST在网页API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建的OData控制器上传文件

I would like to create oData controller to upload files

FileDto


  1. FILEID

  2. NameWithExtension(类型:String)

  3. 元数据(类型:列表)

  4. 内容(类型:流)

========================= HTTP请求的操作==================

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

•GET:〜/文件({ID})

• GET: ~/Files({id})

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

•GET:〜/文件({ID})

• GET: ~/Files({id})

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

•POST:〜/文件

• POST: ~/Files

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

不知道什么时候用的OData加上我如何能做到这一点。

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

在此先感谢

推荐答案

<一个href=\"http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint\"相对=nofollow>此页面说明如何创建一个oDataController。

This page explains how to create an oDataController.

1)以您的项目安装包,打开控制台管理器和输入:

1) To install the package on your project, open the console manager and type this:

Install-Package Microsoft.AspNet.Odata

2)打开你的 WebApiConfig.cs ,并在注册方法,添加这code:

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)创建您的oDataController替换 yourDataSourceHere 来使用你自己的类:

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:为了测试这个解决方案,我改变了 FileDto 的属性内容。更具体地,它的型!从字节[] 。发布内容为Base64字符串。

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.

这篇关于媒体资源支持的OData POST在网页API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆