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?

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

@Obalix: Really? How would that work? I thought it would then only find a controller with the name Route1Controller. Can you elaborate?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.