HttpContext.Current.Request.Form.AllKeys in ASP.NET CORE version

廉价感情. 提交于 2020-12-05 06:38:57

问题


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

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' threw an exception of type 'System.InvalidOperationException'

Converted code:

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

回答1:


Your could use this:

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"



回答2:


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

foreach (var item in data)
{
    if (item.Key.Contains("hello"))
    {
        // ?
    }
    else if (item.Key.Contains("world"))
    {
        // ?
    }
}


来源:https://stackoverflow.com/questions/43284562/httpcontext-current-request-form-allkeys-in-asp-net-core-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!