Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
C#
Which is better for passing variables between pages ? QueryString VS C# class



Why people always use QueryStrings, it's bad solution cuz of Injection... idk if that's true.

why not just C# class to pass variables ( username, userip, etc... )

What's difference in :

- Performance
- Safety
Posted

Well, I'd don't think "people always use" query strings - I only use them for a few specific things, such as email confirmation and so forth. Google uses them quite extensively, but most sites probably don't.

You can't really use a C# class directly to pass values between pages, because they aren't preserved between pages, and there is no mechanism to pass them directly. Instead, you would use either Cookies or (more likely) the Session object to store your data and pass it that way. You could create a C# class and put that in the Session, but it isn't strictly needed.
 
Share this answer
 
QueryStrings are not just bad for the websites (because everything is appended to the URL making it long... Some browsers prefer short URLs), it is also bad for SEOs. You should consider generating URLs like, http://www.example.com/page/param_value.

Secondly, since you are talking about ASP.NET, ASP.NET provides many ways of passing the values, of which, Sessions are much better and fine-tuned ways for passing the values if the data is only going to exist for that Session and does not require to be maintained even if user closes the browser.

How to share data among different web pages using ASP.NET[^], in many ways, Session variables are much recommended to be used. But remember, do not store sensitive information in Cookies or any other storage mechanism. Always use tokens when you want to share the sensitive permissions like credit card information or password details.
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 30-Jan-16 14:12pm    
In this case, the URL which is like http://www.example.com/page?param=value, would be mapped to this: http://www.example.com/page/param_value. That is basically just a string and you can build strings in any way. :-)
Afzaal Ahmad Zeeshan 30-Jan-16 14:25pm    
Use,


var url = string.Format("Questions/{0}", QuestionID);
Response.Redirect(url, false);


In the above code, QuestionID would be a variable that you will have to take from the database or data source.
Sergey Alexandrovich Kryukov 30-Jan-16 21:24pm    
5ed, but why everyone forgets such a good newer mechanism as Web Storage? — please see Solution 3.
—SA
Pay attention for the newer way of passing data between pages, of between the instances of the same page after reloading: Web Storage:
Web storage — Wikipedia, the free encyclopedia[^],
Web Storage API — Web APIs | MDN[^],
Using the Web Storage API — Web APIs | MDN[^].

It has many benefits. It's the most universal because it does not depend on what you have on the server side and works even if you don't use an HTTP sever at all. It is very light weight and does not consume any traffic.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900