General route URL problem

6 views
Skip to first unread message

cathal

unread,
Oct 7, 2009, 7:55:29 AM10/7/09
to S#arp Architecture
Hey,
I don't think this is a S#arp specific question, but perhaps one of
you guys have come across this before?
I've encountered this problem, albeit in different forms, in three MVC
projects I have done.

Problem:
I want to redirect to another controller action, and I want to do this
in a strongly typed manner.

Solution:
I can easily use the MvcContrib extensions here and:
1, Strongly type the controller: RedirectToAction<AnotherController>
2, Strongly type the route values as specified by the action I wish to
redirect to: x => x.Show(searchCriteria.SelectedCompanySlug)

BUT!
Works perfect except when one or more of the action parameters (route
values) are querystring parameters. Perhaps I'm wrong in how I
register the routes - I don't include any query string parameters??
So basically I have to resort to building the URL up in a hard-coded
manner, and now obviously when an extra requirement comes to light,
it's not strongly typed and so the changes aren't uncovered easily.

Any ideas?

Thanks,
Cathal.

cathal

unread,
Oct 30, 2009, 7:32:43 AM10/30/09
to S#arp Architecture
Just in case anybody is reading this, and has the same problem.
I got to the bottom of it, and now realise why I was getting different
flavours of the same problem:

1, The basic RedirectToAction call was not working because it doesn't
account for Areas. S#arp has an extension method for
Html.ActionLinkForAreas, but I suggest that an extension to controller
for RedirectToAction, or perhaps an extension to UrlHelper - i.e. an
extension accessible from the controller (context) that takes Area's
into consideration. I can provide the code here if desired cathal[DOT]
mchale[AT]gmail

2, It doesn't work for posts to a controller that originate from a
partial rendered via Html.RenderAction. E.g. I have my search partial
rendered on the home page via Html.RenderAction. When this posts and
you try to generate a route URL, it just can't map it for some reason
(something to do with the context) - but anyhow, I can live with it
being that the only time I have to Redirect(HardCodedUrl) is for
RenderAction posts. For everything else 1, works in a strongly typed
manner.

3, To make things really clean, I've elaborated on the custom
ModelBinder concept. With a custom ModelBinder for SearchCriteria, I
can simply specify action methods with SearchCriteria as a parameter,
and the custom binder strips the route parameters (in this case query
string params) and converts to a SearchCriteria object.
I took this a step further by having my custom binder inherit an
interface IRoutableObject<T> defining the method: RouteValueDictionary
GetRouteValuesFor(T routeModel); I incorporated this logic into my
redirect extension in 1, and now I can
RedirectToAction<SearchController>(x => x.Results(new SearchCriteria
{ SelectedCountry = "Ireland", SelectedAge = "20" })) - and the
redirect extension does what it used to do, but also checks if the
object is a RoutableObject - if so it'll add the appropriate route
parameters for the objects state.

Cheers,
Cathal.
Reply all
Reply to author
Forward
0 new messages