【问题标题】:No MediaTypeFormatter is available to read an object of type 'HttpRequestMessage' from content with media type 'multipart/form-data'没有 MediaTypeFormatter 可用于从媒体类型为“multipart/form-data”的内容中读取“HttpRequestMessage”类型的对象
【发布时间】:2017-08-29 23:42:14
【问题描述】:

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

在不引用任何其他库的情况下使用该函数时,我无法在 HttpRequestMessage 上使用 ReadAsFormDataAsync。我收到一个:

System.Net.Http.UnsupportedMediaTypeException: No MediaTypeFormatter is 
available to read an object of type 'FormDataCollection' from content with 
media type 'multipart/form-data'. 

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

当我引用 .NET Standard 库时,我什至无法输入该函数,因为它被完全拒绝:

System.Net.Http.UnsupportedMediaTypeException: No MediaTypeFormatter is 
available to read an object of type 'HttpRequestMessage' from content with 
media type 'multipart/form-data'

我尝试创建一个全新的骨架 .NET Standard 库并引用它和相同的东西。

作为附加参考,我找到了this post,但我似乎没有遇到同样的问题。

本来打算提出问题,但决定先在这里尝试。有什么想法吗?

编辑:当 enctype 为 application/x-www-form-urlencoded 时也会发生这种情况。

【问题讨论】:

    标签: c# azure azure-functions


    【解决方案1】:

    据我所知,“ReadAsFormDataAsync”方法只接受“application/x-www-form-urlencoded”类型的内容。它不支持获取 'multipart/form-data' 类型的内容。

    所以如果你想发送多个部分的比赛,你需要使用“ReadAsMultipartAsync”方法。

    关于如何在azure函数中使用“ReadAsMultipartAsync”方法的更多细节,您可以参考以下代码:

    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);
    }
    

    结果:

    【讨论】:

    • 我应该在原始帖子中指出,这种 enctype 也会发生这种情况。我将编辑添加。
    • 在我这边,我可以获得 application/x-www-form-urlencoded 请求。如果可能,请发布您现在使用的整个 azure 函数 http 触发代码。
    • [FunctionName("TestPost")] public static async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequestMessage req, TraceWriter log) { dynamic data = await req. Content.ReadAsAsync();字符串名称=数据?。名称;返回 req.CreateResponse(HttpStatusCode.Created);该函数没有被输入,因为它失败了:异常绑定参数“req”和另一个错误:没有 MediaTypeFormatter 可用于从媒体类型为“application/x-www-form-”的内容中读取类型为“HttpRequestMessage”的对象urlencoded'。
    • 根据您的代码,您无需更改代码即可获得 'application/x-www-form-urlencoded' 值。我建议您可以尝试我的代码并再次测试。
    • 我想你误解了我的意思。我什至无法进入该功能。当使用任一表单类型时,该函数甚至不会形成请求对象。它只是以所述错误拒绝它。从请求对象中获取帖子值很简单
    【解决方案2】:

    您是否尝试在 HTTP 请求标头上将内容类型设置为 JSON?

    Content-Type=application/json
    

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 2018-05-29
      • 2012-09-12
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 2017-08-05
      • 1970-01-01
      相关资源
      最近更新 更多