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!

Brandon McAlees
  • 715
  • 2
  • 12
  • 28
  • 1
    have you considered using [RouteAttributes](http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx)? Makes this a lot easier IMHO – Jonesopolis Oct 22 '15 at 18:20

2 Answers2

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" }
        );
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 2
    A orange upward arrow for suggesting the easiest fix (change the action name) – Tommy Oct 22 '15 at 18:27
  • @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. – Brandon McAlees Oct 22 '15 at 19:22
  • @Steve I switched the order and it still returns plain text HTML with the ambiguousMatch exception, any idea why? – Brandon McAlees Oct 22 '15 at 19:22
  • 2
    @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. http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc – Tommy Oct 22 '15 at 19:29
3

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.

Brandon McAlees
  • 715
  • 2
  • 12
  • 28