asp.net-web-api – Web API中的OData POST的媒体资源支持

我想创建oData控制器来上传文件

FileDto

> FileId
> NameWithExtension(Type:String)
>元数据(类型:列表)
>内容(类型:流)

======================= Http请求操作==================

•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:〜/文件

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

不知道我如何能与OData结合起来.

提前致谢

解决方法

This page解释了如何创建一个oDataController.

1)要将包安装在项目中,请打开控制台管理器并键入以下内容

Install-Package Microsoft.AspNet.Odata

2)打开您的WebApiConfig.cs,并在Register方法添加以下代码

ODataModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<FileDto>("File");            
            config.MapODataServiceRoute(
                routeName: "ODaTaroute",routePrefix: null,model: builder.GetEdmModel());

3)创建您的oDataController替换yourDataSourceHere以使用您自己的类:

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的属性Content.更具体来说,它是类型!从流到字节[].发布内容为Base64字符串.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....