tags:

views:

39

answers:

1

A followup question to: http://stackoverflow.com/questions/3091705/aggregate-on-dictionary-question.

I used the Aggregate Linq functionality but perhaps there is a better, more clean way, to do it?

Can I get the current route from the HtmlHelper? Or what do you suggest? I want to make a language switcher so as when I am on a page/route and click another language the same action gets requested but with another language in the route.

Something like

EN/Home/Index and FR/Home/Index

+1  A: 

You can easily retrieve the current route, or pieces of it. Assuming an HtmlHelper is your context as you say, it should look something like this:

public static MvcHtmlString SomeHelper(this HtmlHelper html) {
    RouteBase route = html.ViewContext.RouteData.Route;
    string action = html.ViewContext.RouteData.Values["action"].ToString();
    string controller = html.ViewContext.RouteData.Values["controller"].ToString();
    // ...
}
Nathan Taylor
The first line does not compile. Route is of type RouteBase so an extra cast was needed. Also fro the Values an extra call for ToString() is needed.
Nyla Pareska
My bad, I'll update the code. I didn't actually test that as you can probably guess. Is that the solution you were looking for in any case?
Nathan Taylor