You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core无Request.Form.GetValues方法,.NET 5下该如何实现

原因说明

.NET 5 中 HttpRequest.Form 的返回类型为 IFormCollection,和 .NET Framework 中的 NameValueCollection 不同,该接口未提供 GetValues 方法,因此原有代码会报错。

解决方案

IFormCollection 的索引器会直接返回对应键的所有值,类型为 StringValues,原生支持转为字符串集合,两种常用写法如下:

  • 写法1:同时校验键不存在、值为空两种场景,等价于原有逻辑且更严谨
if (!StringValues.IsNullOrEmpty(Request.Form["multipleTags"]))
{
    selectedTags = Request.Form["multipleTags"].ToList();
}
  • 写法2:仅判断键存在时取值
if (Request.Form.ContainsKey("multipleTags"))
{
    selectedTags = Request.Form["multipleTags"].ToList();
}

内容的提问来源于stack exchange,提问作者Kosta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 00:57:02