Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
{      
}
like image 727
bbusdriver Avatar asked Apr 07 '17 18:04

bbusdriver


People also ask

How does HttpContext work in asp net core?

The HttpContext encapsulates all the HTTP-specific information about a single HTTP request. When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response.

What is the difference between HttpContext current items and HttpContext current session in asp net?

Current. Item” data is live for single HTTP request/Response where HttpContext. Current. Session data is live throughout user's session.

What is System Web HttpContext current application?

HttpContext. Current. Application is simply a reference to the static global HttpApplicationState object in . NET for your Web Application, of which there should be one global instance per web application. By storing data there, you provide fast, thread-safe access to your global variables.

What is HttpContext request?

The Request property provides programmatic access to the properties and methods of the HttpRequest class. Because ASP.NET pages contain a default reference to the System. Web namespace (which contains the HttpContext class), you can reference the members of HttpRequest on an .


1 Answers

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"
like image 99
Gabriel Robert Avatar answered Sep 25 '22 14:09

Gabriel Robert