Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NETWebAPI 2中使用PUT动词上载文件_C#_Http_File Upload_Asp.net Web Api_Asp.net Web Api2 - Fatal编程技术网

C# 在ASP.NETWebAPI 2中使用PUT动词上载文件

C# 在ASP.NETWebAPI 2中使用PUT动词上载文件,c#,http,file-upload,asp.net-web-api,asp.net-web-api2,C#,Http,File Upload,Asp.net Web Api,Asp.net Web Api2,我想公开一个ASP.NETWebAPI2操作,使用HTTPPUT动词上载文件。这与我们的REST模型是一致的,因为API表示远程文件系统(类似于WebDAV,但实际上是简化的),所以客户机选择资源名称(因此PUT是理想的,POST不是逻辑选择) WebAPI文档描述了,但没有描述如何使用PUT方法实现 您将使用什么来测试这样的API(HTML多部分表单不允许PUT动词)?服务器实现是否与中描述的多部分实现类似(使用MultipartStreamProvider),还是如下所示: [HttpPut

我想公开一个ASP.NETWebAPI2操作,使用HTTPPUT动词上载文件。这与我们的REST模型是一致的,因为API表示远程文件系统(类似于WebDAV,但实际上是简化的),所以客户机选择资源名称(因此PUT是理想的,POST不是逻辑选择)

WebAPI文档描述了,但没有描述如何使用PUT方法实现

您将使用什么来测试这样的API(HTML多部分表单不允许PUT动词)?服务器实现是否与中描述的多部分实现类似(使用
MultipartStreamProvider
),还是如下所示:

[HttpPut]
public async Task<HttpResponseMessage> PutFile(string resourcePath)
{
    Stream fileContent = await this.Request.Content.ReadAsStreamAsync();
    bool isNew = await this._storageManager.UploadFile(resourcePath, fileContent);
    if (isNew)
    {
        return this.Request.CreateResponse(HttpStatusCode.Created);
    }
    else
    {
        return this.Request.CreateResponse(HttpStatusCode.OK);
    }
}
[HttpPut]
公共异步任务PutFile(字符串resourcePath)
{
Stream fileContent=等待这个.Request.Content.ReadAsStreamAsync();
bool isNew=wait.\u storageManager.UploadFile(resourcePath,fileContent);
如果(是新的)
{
返回此.Request.CreateResponse(HttpStatusCode.Created);
}
其他的
{
返回此.Request.CreateResponse(HttpStatusCode.OK);
}
}

经过几次测试,我作为示例发布的服务器端代码似乎是正确的。下面是一个示例,去掉了任何身份验证/授权/错误处理代码:

[HttpPut]
[Route(@"api/storage/{*resourcePath?}")]
public async Task<HttpResponseMessage> PutFile(string resourcePath = "")
{
    // Extract data from request
    Stream fileContent = await this.Request.Content.ReadAsStreamAsync();
    MediaTypeHeaderValue contentTypeHeader = this.Request.Content.Headers.ContentType;
    string contentType =
        contentTypeHeader != null ? contentTypeHeader.MediaType : "application/octet-stream";

    // Save the file to the underlying storage
    bool isNew = await this._dal.SaveFile(resourcePath, contentType, fileContent);

    // Return appropriate HTTP status code
    if (isNew)
    {
        return this.Request.CreateResponse(HttpStatusCode.Created);
    }
    else
    {
        return this.Request.CreateResponse(HttpStatusCode.OK);
    }
}
编辑2018-05-09:

如中所述,如果您计划支持扩展名为(
{filename}.{extension}
)的文件名,而不强制客户端附加尾部斜杠,则需要修改web.config以将IIS绑定到这些文件类型的web api应用程序,默认情况下,IIS将使用静态文件处理程序处理类似文件名的内容(即最后一个路径段包含点的URL)。我的
system.webServer
部分如下所示:


请注意,由于各种限制,某些文件名将无法使用。例如,您不能命名路径段
因为RFC需要替换它,Azure hosting services不允许冒号作为路径段的最后一个字符,IIS默认情况下禁止一组字符

您可能还希望增加IIS/ASP.NET文件上载大小限制:



看看如何为此编写适当的单元测试,而不是依赖于运行HTTP服务器,这将是一件有趣的事情。我一直遇到404错误,因此很难让它正常工作。事实证明,您可能需要更改web.config以接受格式为
{filename}.{extension}
的文件名。有关详细信息,请参阅此问题:
using (var fileContent = new FileStream(@"C:\temp\testfile.txt", FileMode.Open))
using (var client = new HttpClient())
{
    var content = new StreamContent(fileContent);
    content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
    client.BaseAddress = new Uri("http://localhost:81");
    HttpResponseMessage response =
        await client.PutAsync(@"/api/storage/testfile.txt", content);
}