Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Razor Get variable

Tags:

c#

razor

I'm currently working with C# and I want to get the variable of a GET method. (like this: index.cshtml?id=thisvariable)

I'm currently using:

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

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

like image 705
Fumping Avatar asked May 01 '26 03:05

Fumping


1 Answers

In order to get data sent by a GET request, you read the QueryString from the Request.

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

You should also add checks to make sure that the QueryString[KEY] exists, so that you don't get a NullReferenceException when it tries to find a KEY that is not present.

like image 73
Der Kommissar Avatar answered May 02 '26 17:05

Der Kommissar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!