user1007103
user1007103

Reputation: 425

Is Request.QueryString without ? or & possible?

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"];

Upvotes: 2

Views: 436

Answers (3)

Pauli Østerø
Pauli Østerø

Reputation: 6916

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:

Upvotes: 1

competent_tech
competent_tech

Reputation: 44941

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

Take a look at this MSDN documentation for more information.

Upvotes: -1

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25465

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

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

Upvotes: 4

Related Questions