2

Is it possible to get the QueryString value without using ? or & in the url?

I would like to have it like this:

http://www.colors.com/Red

string id = Request.QueryString["?"];

Instead of following:

http://www.colors.com/?ColorID=Red

string id = Request.QueryString["ColorID"];
Pankaj
  • 9,749
  • 32
  • 139
  • 283
user1007103
  • 405
  • 6
  • 11
  • 16
  • 2
    You mean URL Rewriting, there are various ways to do this, here is another post on the matter: http://stackoverflow.com/questions/2375256/url-rewriting-in-net-mvc – Steven de Salas Jan 15 '12 at 20:54
  • 2
    URL Routing http://msdn.microsoft.com/en-us/library/cc668201.aspx – Ahmet Kakıcı Jan 15 '12 at 20:55
  • Note that `URL Rewriting` and `URL Routing` are two different things, which in the end accomplish the same general goal. There are subtle differences in how they operate, which may or may not be important to you. – Andrew Barber Jan 15 '12 at 20:58

3 Answers3

3

No. a query string is defined by the appearance of a ?.

The example you give would redirect the user to a directory.

Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
1

If you want to still be able to access the value of color-id through Querystring, then you should look at Rewriting. This can be due to legacy code that you can't change or other forms of interacting with 3rd party code. The benefit or Rewriting is that the code that ends up being executed doesn't know how the url looked like before it was rewritten and it can keep working as if there were a Querystring parameter named ColorID.

In its simplest form you need to call the Rewrite method of HttpContext, which will spin up a new request internally that executes code that matches that url without the user noticing anything. One caveat of this can be, that your legacy code doesn't know how to render correct links in menus and stuff, so you would keep having urls like ?ColorID=Red where it should have been just Red.

In IIS 7 and up, there is a built in filter where you can write your rules and patterns so you don't need to write your own code that matches incoming requests and calls HttpContext.Rewrite. Read more about it here on MSDN.

Now, Routing is a whole other thing. Its a Asp.net feature and doesn't work on top of existing legacy code but needs to be used with it. Meaning that the executing code needs to know that the request was routed to it. This of course has many benefits and of you're writing a new system then i would definitively recommend using Routing over Rewriting. There is a good article here about the differences and some SO questions also cover the topic:

Community
  • 1
  • 1
Pauli Østerø
  • 6,878
  • 2
  • 31
  • 48
-1

It sounds like you may want to implement an MVC website.

Take a look at this MSDN documentation for more information.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • MVC has nothing to do with the construction of these kind of urls. Its true MVC is centered around it, but it doesn't mean the concept itself solely belongs to MVC. – Pauli Østerø Jan 15 '12 at 22:50
  • The OP was not asking about *construction* of the url, they were asking about *accessing* the URL. The assumption is that they would like to use a URL format in the shown form in order to pass parameter values to the web application. With this set of information, MVC is a perfectly valid suggestion that I would assume, based on the question, the OP had no idea about. – competent_tech Jan 15 '12 at 22:58
  • @PauliØsterø is correct. You can use routes.MapPageRoute to create a route for the pattern the OP showed in "plain old ASP.NET". It has nothing to do with MVC. – Peter Bromberg Jan 15 '12 at 23:31
  • Yes, but I completely and utterly fail to see how this is an invalid answer. Yes, you may be able to do this in plain old ASP.Net, BUT you can ALSO do this in MVC. What on earth could possibly be wrong with that suggestion? – competent_tech Jan 15 '12 at 23:40
  • MVC was presented in a light that made it appear that it was the preferred or only solution, that is all. Routing is available to all ASP.NET type application frameworks. – Peter Bromberg Jan 16 '12 at 00:39
  • This is why I specifically said you **may** want to implement an MVC website and directed the OP to documentation on them so that they could be introduced to the concept and make a determination on their own. In no way did I state or imply that this was the only possible solution. – competent_tech Jan 16 '12 at 00:56
  • you're saying that it sounds he wants MVC, i completely fail to understand how you hear that. He wants cleaner and more semantic urls in asp.net, and the answer for that is Rewriting or Routing. MVC works on top of Routing, yes, but that's not relevant for answering the question. – Pauli Østerø Jan 16 '12 at 13:13
  • I was NOT saying that the OP wants MVC. I simply suggested that MVC **may** be an option to look at (in case they were unaware of it). I can't believe that this is such as difficult concept for people to grasp. What it works on top of is utterly irrelevant to the question, it has the same syntax as what the OP was asking for. – competent_tech Jan 16 '12 at 16:59