I have a form in an asp.net mvc site that serves 3 purposes: paging, sorting, and searching. These items should all be rendered in the same form, since returning the correct search results depends on variables from all 3 aspects. What I'm trying to do is move the parameters out of the querystring and put them in a canonical URL.
I'm almost there, here are my 3 route configurations so far (using T4MVC for area, controller, and action names):
context.MapRoute(null,
"my-area/my-widgets/search/{size}-results-max/page-{page}/order-by-{sort}",
new
{
area = MVC.MyArea.Name,
controller = MVC.MyArea.MyWidgets.Name,
action = MVC.MyArea.MyWidgets.ActionNames.Search,
page = UrlParameter.Optional,
size = UrlParameter.Optional,
sort = UrlParameter.Optional,
}
);
context.MapRoute(null,
"my-area/my-widgets/canonicalize-search",
new
{
area = MVC.MyArea.Name,
controller = MVC.MyArea.MyWidgets.Name,
action = MVC.MyArea.MyWidgets.ActionNames.CanonicalizeSearch,
}
);
context.MapRoute(null,
"my-area/my-widgets",
new
{
area = MVC.MyArea.Name,
controller = MVC.MyArea.MyWidgets.Name,
action = MVC.MyArea.MyWidgets.ActionNames.CanonicalizeSearch,
}
);
The form in the view submits to the CanonicalizeSearch route, using this syntax:
@using (Html.BeginForm(MVC.MyArea.MyWidgets.CanonicalizeSearch(),
FormMethod.Get))
In the MyWidgetsController, there are 2 action methods:
[ActionName("canonicalize-search")]
public virtual RedirectToRouteResult CanonicalizeSearch(string keyword,
int page = 1, int size = 10, string sort = "Title-Ascending")
{
var result = RedirectToRoutePermanent(new
{
area = MVC.MyArea.Name,
controller = MVC.MyArea.MyWidgets.Name,
action = MVC.MyArea.MyWidgets.ActionNames.Search,
page = page,
size = size,
sort = sort,
keyword = keyword,
});
return result;
}
[ActionName("search")]
public virtual ViewResult Search(string keyword,
int page = 1, int size = 10, string sort = "Title-Ascending")
{
// code to perform query
return View(model);
}
This works for moving all querystring variables into a canonicalized route except for the keyword. If I add a keyword parameter to the first route, the CanonicalizeSearch action only redirects to the Search action when keyword is not null, empty, or whitespace. This is no good as it makes browsing page results impossible when there is no keyword entered.
I think I've tried everything -- giving the keyword a default value in the controller, adding a 4th route that adds keyword to the other 3 parameters, etc. However the only way I can seem get this to work is by keeping keyword as a querystring parameter. (Actually I can get it to work by prepending an underscore to the keyword in CanonicalizeSearch and stripping it off in Search, but that's pretty hacky).
Any help?