c# - Handle Multiple action with same name in MVC -
in project there action
public actionresult lead(int leadid) {     return view(); } and in view actionlink created this
@html.actionlink("old link", "lead", "home", new { leadid = 7 }, null) but after time, make clean url, have changed name of parameter of 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 shared in multiple social network sites. whenever clicks on old link, redirect page www.xyx.com/home/lead?leadid=7
but in application, no such url exists.
to handle problem, thinking of overloading, mvc action doesn't support overloading.
i have created action same name parameter, , redirect new action, doesn't work.
public actionresult lead(int leadid, int extra=0) {     return redirecttoaction("lead", "home", new { id = leadid }); } i have found 1 link handle such situation, not working in case.
one possibility handle write 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 register before 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 have single action:
public actionresult lead(int id) {     return view(); } now both following urls work expected:
- www.xyx.com/home/lead/7
- www.xyx.com/home/lead?leadid=7
Comments
Post a Comment