Webapi& Odata-适用于文件管理? [英] WebApi & Odata - suitable for file management?

查看:81
本文介绍了Webapi& Odata-适用于文件管理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将项目中的大部分/全部API从纯" WCF迁移到Odata,并为此使用了OWIN托管的Odata.

I am moving most / all of my API in a project over to Odata from "pure" WCF and using an OWIN hosted Odata enpoint for this.

此刻我坚持使用的一个元素是文件.我有2个区域需要将ZIP文件上传到服务器进行处理.在一种情况下,它连接到实体(称为存储库"),并且包含不通过Odata公开的二进制内容(只是上载).另一方面,这是用于未绑定的操作,ZIP文件包含将创建/更改多个实体的配置文件.

The one element I am stuck with at the moment are files. I have 2 areas where I need to upload a ZIP file to the server for processing. In one case that is attached to an Entity (called a "Repository") and contains binary content that is not exposed via Odata (it is just uploaded). In the other hand this is for an unbound action and the ZIP file contains configuration files which will crate/change a number of entities.

使用OData可行吗,还是应该忽略Odata并使用手动配置"标准端点?由于公开了元数据,我真的很想将其保留在Odata中.

Is that feasible with OData, or should I ignore Odata for this and go with "Manually configured" standard endpoints? I really would love to keep this in Odata due to the exposed metadata.

在任何人发表评论之前-我一直在尝试通过Google查找文档,但始终没有得到相关的答案.我得到的答案表明这是可能的,但是当我使用WebApi时,所有示例均指向旧的WCF级别API. http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-端点不会涉及太多细节.它没有显示动作的Parameter配置所允许的类型,也没有显示如何配置它以使其通过Web表单(和客户端,因为我需要两者)通过http post接受文件.

Before anyone comments - I have been trying to find documentation via google, but I keep getting no relevant answers. The answers I do get indicate this is possible, but all have code examples that point to the old WCF level API, while I use WebApi. The documentation at http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint does not go into too many details. It does not show the allowed types for a Parameter configuration for an action and how to configure it to accept the file via http post from a web form (and a client because I will need both).

推荐答案

以下是有关Web API中OData的媒体资源支持的文档的有用链接:

Here is a useful link with documentation about Media Resource Support for OData in Web API: https://blogs.msdn.microsoft.com/mrtechnocal/2013/10/31/media-resource-support-for-odata-in-web-api/

您可以简化链接中建议的实现,但是可以肯定的是,您将需要:

You can simplify a little bit the implementation proposed in the link, but for sure, you will need to:

  • 创建一个OData媒体资源控制器.它可能比文档中提出的简单.参见下文.
  • 创建一个自定义EntityRoutingConvention以正确地路由到将返回ZIP文件的Get方法(如果有这种用例,如果只需要发布它们,则可能不需要自定义路由约定).

因此,对于控制器,您可以:

So, for the controller, you can have:

public abstract class YourMediaResourceODataController<TEntity, TKey>
    : ODataController where TEntity : class
{
}

然后是真正的控制器:

public class YourController : YourMediaResourceODataController<YourZIPObjectEntity, string>
{
    // This would be the post
    public async Task<IHttpActionResult> Post()
    {
        var stream = await Request.Content.ReadAsStreamAsync();
        // Manage the stream
    }

    // The get (if you want it, you will need to code the custom EntityRoutingConvention).
    [HttpGet]
    public HttpResponseMessage GetMediaResource(string key)
    {
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

        var theZIPFile = yourZIPFileService.GetZIPFileByKey(key);
        StreamContent contentResult;
        using(var ms = new MemoryStream(theZIPFile.theByteArray)
        {
            contentResult = new StreamContent(ms);
        }

        result.Content = contentResult;
        return result;
    }
}

您将需要具有一个Stream/byte []/string属性的实体YourZIPObjectEntity,具体取决于您对二进制文件的管理方式. (在文档示例中,这是Image类).对于该实体,您需要指定它在ODataConfig中具有流(请参见文档中的设置Web API配置"部分).

You will need to have an entity YourZIPObjectEntity with a Stream/byte[]/string property, depending on how you manage the binary file. (In the documentation example this is the Image class). And for that entity you will need to specify that it has a stream in the ODataConfig (see the "Setting Up the Web API Configuration" section in the documentation).

我认为几乎全部.

然后,您可以从代码中将您的ZIP文件作为StreamContent发布:

Then, you can, from code, POST your ZIP files as a StreamContent:

using(var requestContent = new MemoryStream(yourByteArray))
using(var request = new HttpRequestMessage(HttpMethod.POST, yourPOSTUri)
{
    request.Content = new StreamContent(requestContent);
    // Set headers and send the request...
}

我希望这是您正在寻找的解决方案,或者至少是一种解决方法.

I hope this is the solution you are looking for, or at least an approach to it.

这篇关于Webapi&amp; Odata-适用于文件管理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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