I want my searches like those in Stack Overflow (i.e. no action, no slashes):
mydomain.com/search --> goes to a general search page
mydomain.com/search?type=1&q=search+text --> goes to actual search results
My routes:
routes.MapRoute(
"SearchResults",
"Search/{*searchType}", --> what goes here???
new { action = "Results" }
);
routes.MapRoute(
"SearchIndex",
"Search",
new { action = "Index" }
);
My SearchController has these actions:
public ActionResult Index() { ... }
public ActionResult Results(int searchType, string searchText) { ... }
The search results route does not work. I don’t want to use the “…/…” approach that everyone seems to be using, because a search query is not a resource, so I want the data in a query string as I’ve indicated, without slashes–exactly like SO does.
TIA!Matt
You don’t need two routes because you’re providing search parameters as query string. Just have one search route:
Then write this controller action
The body of this method greatly depends on the search condition whether you require both parameters when you search for stuff or just
qand you have some default fortype. Remember that page indexing can be done just the same way.Using strong type parameters (validation wise)
You could of course create a class that could be validated, but property names should reflect that of the query string. So you’d either have a class:
And use the same request with equally named query variables as you do now, or have a clean class and adjust your request: