0

I have a simple controller like this (no, not really, but let's say I do)

public class SomethingController : Controller {
    public ActionResult Method1() {
        return View("Something1");
    }

    public ActionResult Method2() {
        return View("Something2");
    }
}

Now I want to use this controller with two different routes:

public static void RegisterRoutes(RouteCollection routes) {
    routes.MapRoute("Route 1", "Route1/{action}", new { controller = "Something" });
    routes.MapRoute("Route 2", "Route2/{action}", new { controller = "Something" });
}

Up until here, nothing special. However, inside my view Something1 I now want to do do

Html.ActionLink("Do Something", "Method2")

and this should render <a href="Route1/Method2"... or <a href="Route2/Method2"..., depending on which route led to the controller that displayed the view. How can this be done?

flag

67% accept rate

1 Answer

2

Use Html.RouteLink instead of Html.ActionLink. It lets you specify the route name.

link|flag
@Obalix: Really? How would that work? I thought it would then only find a controller with the name Route1Controller. Can you elaborate? – erikkallen Feb 18 at 21:36

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.