gpt4 book ai didi

c# - ASP.NET CORE 版本中的 HttpContext.Current.Request.Form.AllKeys

转载 作者:太空狗 更新时间:2023-10-30 00:30:00 25 4
gpt4 key购买 nike

foreach (string key in HttpContext.Current.Request.Form.AllKeys)
{
string value = HttpContext.Current.Request.Form[key];
}

以上代码的.net core版本是多少?似乎 .net 核心取出了 AllKeys 并将其替换为 Keys 。我尝试将上面的代码转换为.net core方式,但是它抛出了一个无效操作异常。

HttpContext.Request.Form = 'HttpContext.Request.Form' threw an exception of type 'System.InvalidOperationException'

转换后的代码:

foreach (string key in HttpContext.Request.Form.Keys)
{
}

最佳答案

你可以用这个:

var dict = Request.Form.ToDictionary(x => x.Key, x => x.Value.ToString());

在这种情况下,您可以遍历字典或直接访问值:

dict["Hello"] = "World"

关于c# - ASP.NET CORE 版本中的 HttpContext.Current.Request.Form.AllKeys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43284562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com