Question

The new ASP.NET routing is great for simple path style URL's but if you want to use a url such as:

http://example.com/items/search.xhtml?term=Text+to+find&page=2

Do you have to use a catch all parameter with a validation?

Was it helpful?

Solution

Any view data items that are not listed in the route are automatically mapped to the querystring, so if you map "items/search.xhtml" to an action:

Search(string term, int page)

Then you should get the results you are looking for.

OTHER TIPS

You can match querystring parameters with routes as well, if you want to just capture everything you need to add a parameter like so:

{*contentUrl}

Which will populate the rest of the url into that variable.

I was also having trouble passing an encoded URL to a route as a route parameter.

You can't use url encoded chars in a URL, but you can in a query string.

Therefore I needed my route to also have a query string element to it.

Say I have a route:

MapPageRoute("myroute", "myroute/{x}", "~/routehander.aspx")

But I want it in the form of:

http://mywebsite.com/myroute/{x}?url=myurl

We can do this:

Dim x as integer = 12
Dim rvd As New Routing.RouteValueDictionary
rvd.Add("x", x)
rvd.Add("url", Server.UrlEncode("/default.aspx"))
HttpContext.Current.ApplicationInstance.Response.RedirectToRoutePermanent("myroute", rvd)

This would redirect us to the following url:

http://mywebsite.com/myroute/12?url=%252fdefault.aspx

You can still use Request.QueryString["some_value"];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top