Azure functions - How to read form data(Azure 函数 - 如何读取表单数据)
问题描述
如何在Azure函数中读取表单数据?我尝试了几种方法,但总是出现错误,例如:
How to read form data in Azure functions? I tried to do it in several ways, but always I get an error, eg.:
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
dynamic data = await req.Content.ReadAsFormDataAsync();
return req.CreateResponse(HttpStatusCode.OK, $" {data}");
}
错误:执行函数时出现异常:Functions.FormTrigger.System.Net.Http.Formatting:没有 MediaTypeFormatter 可用于从媒体类型为application/json"的内容中读取FormDataCollection"类型的对象.
我检查了请求内容,我收到的请求是 multipart/form-data
:
I checked request content and I'm getting request as multipart/form-data
:
" ------WebKitFormBoundary47wKq7pk9Fcc4H9J
Content-Disposition: form-data; name="name"
sdgs
------WebKitFormBoundary47wKq7pk9Fcc4H9J
Content-Disposition: form-data; name=" _replyto"
sdg@sdg.com
------WebKitFormBoundary47wKq7pk9Fcc4H9J
Content-Disposition: form-data; name="message"
sdgsd
------WebKitFormBoundary47wKq7pk9Fcc4H9J--
"
感谢您的任何提示.
推荐答案
由于请求包含application/x-www-form-urlencoded"类型的内容,需要将输入转换为NameValueCollection才能读取输入:
As the request contains "application/x-www-form-urlencoded" type of contents, you need to convert the input to NameValueCollection in order to read input:
NameValueCollection col = req.Content.ReadAsFormDataAsync().Result;
return req.CreateResponse(HttpStatusCode.OK, $" {col[0]}");
您还可以传递 Key 字符串而不是 Index,这将使代码更具可读性和不言自明
You can also pass Key string instead of Index which would make the code more readable and self-explanatory
这篇关于Azure 函数 - 如何读取表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Azure 函数 - 如何读取表单数据


基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01