![](/img/trans.png)
[英]No MediaTypeFormatter is available to read an object of type 'Product' from content with media type 'text/plain'
[英]No MediaTypeFormatter is available to read an object of type 'HttpRequestMessage' from content with media type 'multipart/form-data'
我正在調整最近在.NET Standard中啟動的項目以使用Azure Functions。 我有一個HTTP觸發器,可以直接從表單中發布到該觸發器。 只有2個字段:數字輸入和文件上傳。
當使用該函數而不引用任何其他庫時,我無法在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標准庫時,由於它被完全拒絕,我什至無法輸入該函數:
System.Net.Http.UnsupportedMediaTypeException: No MediaTypeFormatter is
available to read an object of type 'HttpRequestMessage' from content with
media type 'multipart/form-data'
我嘗試創建一個全新的.NET標准框架,並引用相同的內容。
作為其他參考,我發現了這篇文章 ,但似乎沒有相同的問題。
打算提出問題,但決定先在這里嘗試。 有任何想法嗎?
編輯:當enctype為application / x-www-form-urlencoded時,也會發生這種情況。
據我所知,“ 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);
}
結果:
您是否嘗試過在HTTP請求標頭上將內容類型設置為JSON?
Content-Type=application/json
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.