views:

177

answers:

5

I'm developing a web app that has a database backend. In the past I'm done stuff like: http://page.com/view.aspx?userid=123 to view user 123's profile; using a querystring.

Is it considered good practice to use a querystring? Is there something else I should be doing?

I'm using C# 4.0 and ASP.net.

+3  A: 

Query String are perfectly fine if you're sure to lock down what people are meant to view.. You should be checking for a valid value (number, not null, etc..) and if your application has security, whether a Visitor has permission to view User 1245's profile..

You could look into Session & ViewState, but QueryString seems to be what you're after.

Marko
+6  A: 

Your question isn't really a .NET question... it is a concern that every web framework and web developer deals with in some way.

Most agree that for the main user facing portion of your website you should avoid long query strings in favor of a url structure that makes "sense" to the website visitor. Try to use a logical hierarchy that when the visitor reads it there is a good chance they can deduce where they are on the site. Click around StackOverflow in a few areas and see what they have done with the url's. You usually have a pretty good idea what you're looking at and where you are.

A couple of other heads up... Although a lot of database lookups are done with the primary key it's also a good idea to provide a user friendly name of the resource in your url instead of just the primary key. You see StackOverflow doing that in the current address where they're doing the lookup with the primary key "3544483" but also including an SEO/user friendly url paramenter "are-querystrings-in-net-good-practice." If someone emailed you that link you'd have a pretty good idea of what you're about to open up.

I'm not really sure how WebForms handles Url Routing but if you're struggling to grasp the concepts go through the MVC NerdDinner tutorial. They cover some basic url routing in there that could help.

DM
+1  A: 

If possible, I think this practice should be avoided especially if you're passing auto-incrementing ids in plain text. In my opinion, you're almost teasing the user to manipute the querystring value and see if they can get access to someone else's profile. Even with appropriate security measures in place (validating the request on the server-side before rendering the page), I would still recommend encrypting the querystring param in this particular case.

Ben Griswold
A: 

I think using query strings is perfectly fine, but there's a case to be made for hackable URLs, in that they are more understandable to advanced users and are SEO-friendly. For example, I happen think http://www.example.com/user/view/1234 looks more intuitive than http://www.example.com/view.aspx?user=1234.

And you don't have to alter your application to use pretty URLs if you're using IIS 7.0. The URL Rewrite Module and a few rewriting rules should be enough.

ShaderOp
The URL rewrite module looks awesome. Have you used it before?
Morgan
Yes, I have used it a few times. It's fully supported by Microsoft, and, aside from having to know regular expressions, you have nothing to worry about using it.
ShaderOp
A: 

To answer clearly at your question: yes it't a good pratice. In fact it's an expected behavior of a web site.

I'm totaly agree with ShaderOp and you should use a url rewritter to get an nice loocking url. In fact I'm assuming that you will put a bit of validation to avoid someone manipulating the url and access to data they don't desserve.

Muffun