本文介绍了没有MediaTypeFormatter可用于从媒体类型为"multipart/form-data"的内容中读取类型为"HttpRequestMessage"的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在改编一个最近在.NET Standard中启动的项目以使用Azure Functions.我有一个HTTP触发器,可以直接从表单中发布到该触发器.只有2个字段:一个数字输入和一个文件上传.

使用该函数而不引用任何其他库时,不能在HttpRequestMessage上使用ReadAsFormDataAsync.我收到:

  System.Net.Http.UnsupportedMediaTypeException:没有MediaTypeFormatter是可用于从内容中读取类型为"FormDataCollection"的对象媒体类型"multipart/form-data". 

我可以使用ReadAsMultipartAsync并设法获取帖子值.

但是当我引用.NET Standard库时,由于完全被拒绝,我什至无法输入该函数:

  System.Net.Http.UnsupportedMediaTypeException:没有MediaTypeFormatter是可用于从内容中读取类型为"HttpRequestMessage"的对象媒体类型"multipart/form-data" 

我尝试创建一个全新的.NET标准框架,并引用相同的内容.

作为其他参考,我发现了

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"的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 17:56