C # Razor Get Variable

I am currently working with C # and I want to get a variable of a GET method. (for example: index.cshtml? id = thisvariable)

I am currently using:

if (HttpContext.Current.Request.HttpMethod == "GET")

      

And it works, but I don't know how to get the variable.

+3


source to share


1 answer


To get the data sent by the request GET

, you read QueryString

from Request

.

var id = HttpContext.Current.Request.QueryString["id"];

      



You should also add checks to make sure it exists QueryString[KEY]

so that you don't get NullReferenceException

it when it tries to find KEY

which is not.

+4


source







All Articles