Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access HTTP request information from another class than the code behind

Tags:

c#

asp.net

I was wondering something. In ASP.NET we can have Inside aspx page something like this :

string ref = Request["ref"];

We can have the same thing going in code behind Inside the load of our page with the same type of request.

But is it possible to retreive this value in a C# class (admiting something like toto.cs) ?

Because a Request won't work on a .cs, we already supose to be in the serveur side.

So how would it be done ?.

Edit :

seeing answer and comment, I should have had that toto.cs in this case is not link with the aspx page. It is only a random .cs class in the App_Code repository in a Web project.

like image 373
Slayner Avatar asked Sep 06 '25 12:09

Slayner


1 Answers

Assuming the toto.cs will always be used in conjunction with ASP.Net, you can use the HttpContext object to get the current context and access the Request as in the code behind of a page.

Something like HttpContext.Current.Request.QueryString["ref"];.

HttpContext is located in System.Web.

like image 182
The Sharp Ninja Avatar answered Sep 08 '25 21:09

The Sharp Ninja