简体   繁体   中英

Where should I add an (url encoded) querystring in my named route?

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?

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"
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM