views:

478

answers:

5

Is there a way I can remove null or empty keys from a query string in asp.net MVC? For example I have a page where I filter data on a results table, if I search for John the query string would be redisplayed as:

candidates?FirstName=John&LastName=&Credit=false&Previous=false&Education=&Progress=

and not

candidates?FirstName=John

I looked at URL routing but I wasn't sure if it was something that should be used for cosmetic things like this or if it is possible to achieve what I'm asking using it.

A: 

Whatever URL generator, or control you are using would need special logic to strip these unwanted tags from the list. It's not obvious to a generic URL generator or control that Credit=false is useless -- couldn't it be Credit=true is the default? Similarly an empty string can mean something. (Also, Lastname= is different from Lastname.

Adam Luter
I knew it would fall to me to write the actual logic for it, but I'm not sure where or how it should be handled, with URL routing, within the controller, or done through IIS with an add-on, something similar to Apache's mod_rewrite.
Graham
Oh I just noticed you are using asp.net-MVC, that makes your question clearer. However, it also makes me unqualified to answer, sorry :) .
Adam Luter
+2  A: 

How are you generating that URL? With Routing, if those are meant to be in the query string, it should work fine. We only generate query string parameters for RouteValues that you specify.

One thing I've done in the past is to write my own helper method for specific links where I might pass in an object for route values, but want to clear out the values that I don't need before passing it to the underlying routing API. That worked well for me.

Haacked
In this case it isn't being generated by the route, just a form using the get method to refine the query. Is there a better way to handle this?
Graham
It's been a while since I asked this question but this ended up being the solution I went with, thank you.
Graham
A: 

I sometimes need to work on my route values in partials that are used by variuos views.

Then I usualy access the routeDictionary and change it. The benefit you get is, that there is a good chance that the code will survive changes in the routing and that you can use routeValues in multiple generated URL.

Most people would argue that the best place for this code is not the view. But hopefully you get the idea.

The view code:

   RouteValueDictionary routeValues = ViewContext.RouteData.Values;
   routeValues.Remove(...);
   routeValues.Add(...);
   routeValues["Key"] = ...;

   <%
         using (Html.BeginForm(
             Url.RequestContext.RouteData.GetRequiredString("Action"),
             Url.RequestContext.RouteData.GetRequiredString("Controller"),
             routeValues,
             FormMethod.Get))
   { %>
Malcolm Frexner
A: 

Maybe use this Querystring Builder - iterate querystrings in the Request.QueryString dictionary and build a new one using the builder (or just string-concat them)?

Jon Galloway
A: 

See this Post to see exactly how this is done in asp.net mvc.

Bikal Gurung