1

I have two methods in the SurveyController:

public ActionResult Index(string surveyId, string surveyWaveId, string programId)

and

public ActionResult Index(string hash)

I would like to be able to go to:

Survey/Index/{string} or Survey/Index/{string}/{string}/{string}

and route to the correct action based on the number of parameters supplied. Is this possible with MVC5? I also have this in my RouteConfig.

routes.MapRoute(
            name: "SurveyEmailLink",
            url: "Survey/Index/{hash}",
            defaults: new { controller = "Survey", action = "Index"},
            namespaces: new[] { "Cobalt.Controllers" }
            );

        routes.MapRoute(
            name: "SurveyIconLink",
            url: "Survey/Index/{surveyId}/{surveyWaveId}/{programId}",
            defaults: new { controller = "Survey", action = "Index" },
            namespaces: new[] { "Cobalt.Controllers" }
            );

        routes.MapRoute(
             name: "Default",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
             namespaces: new[] { "Cobalt.Controllers" }
             );

Thanks in advance!

1
  • 1
    have you considered using RouteAttributes? Makes this a lot easier IMHO Commented Oct 22, 2015 at 18:20

2 Answers 2

4

For my specific problem, I could only use RouteAttributes as suggested by Jonesopolis and Tommy.

I ended up adding

[Route("Survey/View/{surveyId}/{surveyWaveId}/{programId}")]

and

[Route("Survey/View/{hash}")]

above my controller actions, and added

routes.MapMvcAttributeRoutes();

to the RouteConfig.

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

1 Comment

4

The best (and easiest) option would be to name your actions to say a bit more about what they are for... unless Index really is the appropriate name for both of these.

    routes.MapRoute(
        name: "SurveyIconLink",
        url: "Survey/Icon/{surveyId}/{surveyWaveId}/{programId}",
        defaults: new { controller = "Survey", action = "Index" },
        namespaces: new[] { "Cobalt.Controllers" }
        );

    routes.MapRoute(
        name: "SurveyEmailLink",
        url: "Survey/Email/{hash}",
        defaults: new { controller = "Survey", action = "Index"},
        namespaces: new[] { "Cobalt.Controllers" }
        );

4 Comments

A orange upward arrow for suggesting the easiest fix (change the action name)
@Tommy both these action's return the same result, they just use different information to get there. So I would like them to be named the same for consistency.
@Steve I switched the order and it still returns plain text HTML with the ambiguousMatch exception, any idea why?
@BrandonMcAlees - You cannot have two ActionMethods defined named the same as the model binder cannot use the parameter list to distinguish between the two. You have to use attribute routing (as suggested in a comment under your question) or rename on of the action methods. stackoverflow.com/questions/436866/…

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.