ASP.NET CORE版本中的HttpContext.Current.Request.Form.AllKeys [英] HttpContext.Current.Request.Form.AllKeys in ASP.NET CORE version

查看:910
本文介绍了ASP.NET CORE版本中的HttpContext.Current.Request.Form.AllKeys的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

上述代码的.net核心版本是什么?似乎.net core删除了 AllKeys ,而是将其替换为 Keys .我试图将上面的代码转换为.net核心方式,但是它引发了无效的操作异常.

What is the .net core version of the above code? Seems like .net core took out AllKeys and replaced it with Keys instead. I tried to convert the above code to the .net core way, but it throws an invalid operation exception.

HttpContext.Request.Form ='HttpContext.Request.Form'引发了类型为'System.InvalidOperationException'的异常

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

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

In that case, you can iterate over your dictionary or you can access values directly:

dict["Hello"] = "World"

这篇关于ASP.NET CORE版本中的HttpContext.Current.Request.Form.AllKeys的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆