3

In my project there is an action

public ActionResult Lead(int leadId)
{
    return View();
}

and in the View an ActionLink was created like this

@Html.ActionLink("Old Link", "Lead", "Home", new { leadId = 7 }, null)

But after some time, to make clean URL, I have changed the name of parameter of that action

public ActionResult Lead(int id)
{
    return View();
}

And ActionLink change accordingly

@Html.ActionLink("New Link", "Lead", "Home", new { id = 5 }, null)

But old link was shared in multiple social network sites. Whenever anyone clicks on that old link, he is redirect to the page www.xyx.com/Home/Lead?leadId=7

But now in my application, no such URL exists.

To handle this problem, I was thinking of overloading, but MVC action doesn't support overloading.

I have created another Action with same name with extra parameter, and redirect to new action, but it doesn't work.

public ActionResult Lead(int leadId, int extra=0)
{
    return RedirectToAction("Lead", "Home", new { id = leadId });
}

I have found one link to handle such situation, but It is not working in my case.

ASP.NET MVC ambiguous action methods

Community
  • 1
  • 1
Amit Kumar
  • 5,888
  • 11
  • 47
  • 85

1 Answers1

2

One possibility to handle this would be to write a custom route:

public class MyRoute : Route
{
    public MyRoute() : base(
        "Home/Lead/{id}",
        new RouteValueDictionary(new
        {
            controller = "Home",
            action = "Lead",
            id = UrlParameter.Optional,
        }),
        new MvcRouteHandler()
    )
    {
    }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var rd = base.GetRouteData(httpContext);
        if (rd == null)
        {
            return null;
        }

        var leadId = httpContext.Request.QueryString["leadid"];
        if (!string.IsNullOrEmpty(leadId))
        {
            rd.Values["id"] = leadId;
        }

        return rd;
    }
}

that you will register before the default one:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.Add(new MyRoute());

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

and now you could only have a single action:

public ActionResult Lead(int id)
{
    return View();
}

Now both the following urls will work as expected:

  • www.xyx.com/Home/Lead/7
  • www.xyx.com/Home/Lead?leadId=7
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928