No MediaTypeFormatter is available to read an object of type #39;HttpRequestMessage#39; from content with media type #39;multipart/form-data#39;(没有 MediaTypeFormatter 可用于从媒体类型为“multipart/form-data的内容中读取“HttpRequestMessage类型的对象)
问题描述
我正在调整最近在 .NET Standard 中启动的项目以使用 Azure Functions.我有一个 HTTP 触发器,我直接从表单发布到该触发器.只有两个字段:数字输入和文件上传.
在不引用任何其他库的情况下使用该函数时,我无法在 HttpRequestMessage 上使用 ReadAsFormDataAsync.我收到一个:
System.Net.Http.UnsupportedMediaTypeException: 没有 MediaTypeFormatter可用于从内容中读取FormDataCollection"类型的对象媒体类型多部分/表单数据".
我可以使用 ReadAsMultipartAsync 并设法获取帖子值.
当我引用 .NET Standard 库时,我什至无法输入该函数,因为它被完全拒绝:
System.Net.Http.UnsupportedMediaTypeException: 没有 MediaTypeFormatter可用于从内容中读取HttpRequestMessage"类型的对象媒体类型多部分/表单数据"
我尝试创建一个全新的骨架 .NET Standard 库并引用它和相同的东西.
作为附加参考,我发现
I'm adapting a project recently started in .NET Standard to use Azure Functions. I have an HTTP trigger that I am posting directly to from a form. There are only 2 fields: a number input, and a file upload.
When using the function without referencing any other libraries, I cannot use ReadAsFormDataAsync on the HttpRequestMessage. I receive a:
System.Net.Http.UnsupportedMediaTypeException: No MediaTypeFormatter is
available to read an object of type 'FormDataCollection' from content with
media type 'multipart/form-data'.
I can use ReadAsMultipartAsync and manage to get the post values.
When I reference a .NET Standard library though, I can't even enter the function as it gets completely rejected:
System.Net.Http.UnsupportedMediaTypeException: No MediaTypeFormatter is
available to read an object of type 'HttpRequestMessage' from content with
media type 'multipart/form-data'
I tried creating a brand new skeleton .NET Standard library and referencing that and same thing.
As an additional reference I found this post, but I don't seem to be having the same issue.
Was going to file an issue but decided to try here first. Any ideas?
EDIT: This also happens when the enctype is application/x-www-form-urlencoded.
As far as I know, the "ReadAsFormDataAsync" method only accepts "application/x-www-form-urlencoded" type of contents. It doesn't support get the 'multipart/form-data' type of contents.
So if you want to send multiple part of contests, you need use "ReadAsMultipartAsync" method.
More details about how to use "ReadAsMultipartAsync" method in azure function, you could refer to this codes:
using System.Net;
using System.Net.Http;
using System.IO;
using System.Collections.Specialized;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string result = "- -";
if (req.Content.IsMimeMultipartContent())
{
var provider = new MultipartMemoryStreamProvider();
req.Content.ReadAsMultipartAsync(provider).Wait();
foreach (HttpContent ctnt in provider.Contents)
{
//now read individual part into STREAM
var stream = ctnt.ReadAsStreamAsync();
return req.CreateResponse(HttpStatusCode.OK, "Stream Length " + stream.Result.Length);
using (var ms = new MemoryStream())
{
//do something with the stream
}
}
}
if (req.Content.IsFormData())
{
NameValueCollection col = req.Content.ReadAsFormDataAsync().Result;
return req.CreateResponse(HttpStatusCode.OK, $" {col[0]}");
}
// dynamic data = await req.Content.ReadAsFormDataAsync();
// Fetching the name from the path parameter in the request URL
return req.CreateResponse(HttpStatusCode.OK, "Doesn't get anything " + result);
}
Result:
这篇关于没有 MediaTypeFormatter 可用于从媒体类型为“multipart/form-data"的内容中读取“HttpRequestMessage"类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:没有 MediaTypeFormatter 可用于从媒体类型为“multipart/form-data"的内容中读取“HttpRequestMessage"类型的对象
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01