views:

627

answers:

1

In my asp.net-mvc ontroller I accept a bunch of form field values and from those create a string the Lucene library understands. Then I want to redirect to a get method that will show results depending on this lucene string.

The lucene string is in the pattern {fieldName1:value1 fieldName2:value2 ...}

my Global.asax has the following entry for the redirect target:

routes.MapRoute(
    "AdvancedSearch",
    "AdvancedSearch.mvc/{displayType}/luceneString",
    new { controller = "Search", action = "AdvancedSearch",
          displayType = "chip", luceneString = "" }
);

So my controller catches the post, creates the luceneString and does the following:

return RedirectToRoute("AdvancedSearch", new
    {
        Controller = "Search",
        action = "AdvancedSearch",            
        displayType = "chip",
        queryString = Url.Encode("company:test name:testname")
     });

This gives me a 500: bad request. Even with one parameter it doesn't work. Even with the ":" it doesn't work.
I tried:

  • AdvancedSearch.mvc/chip/company%3Atest+name%3AtestName
  • AdvancedSearch.mvc/chip/company:test+name:testName
  • AdvancedSearch.mvc/chip/company:test
  • AdvancedSearch.mvc/chip/company%3Atest

It only works if I change this url to take the queryString in the format of

AdvancedSearch.mvc/chip?q=company%3Atest+name%3AtestName

What should I do to get the encoding right without resorting to "?q="
If I have to use the querystring, how can I define such a thing in the route table? How do I go about to call redirect to it?

+1  A: 

First, your MapRoute should contain nothing regarding the query string. Routes contain the resource portion of the URI only; they do not include the query.

Second, you don't need to encode the query string; ActionLink/RouteLink/etc. will do that for you. When you are building an HREF, any tokens not contained in the route will become encoded query string parameters automatically.

Remove:

/luceneString

...from your route.

Change your code to:

return RedirectToRoute("AdvancedSearch", new
{
    Controller = "Search",
    action = "AdvancedSearch",            
    displayType = "chip",
    q = "company:test name:testname"
 });
Craig Stuntz
Thanks, that works. But why can't my luceneString be part of the url? How will this influence caching? Is caching dependant on the querystring too?
borisCallens
If you want it in the resource portion of the URI, it's part of the route. If you want it in the query string portion of the URI, it is not part of the route. Generally, caching is dependent on the whole URI, including the query string, but it depends what sort of caching you're asking about.
Craig Stuntz
The default IIS caching.
borisCallens
In IIS6, no; IIS 7, yes: http://learn.iis.net/page.aspx/204/http-response-cache/ If you actually meant the ASP.NET cache, it depends on your VaryByParam: http://msdn.microsoft.com/en-us/library/system.web.ui.outputcacheparameters.varybyparam.aspx
Craig Stuntz
I would firstly want IIS to worry about if possible so I don't have to. But indeed I can always create a custom varyby if I want.
borisCallens