Redirect to another controller+ action without changing URL in ASP.Net MVC3 -
Redirect to another controller+ action without changing URL in ASP.Net MVC3 -
note: below little demo sort simulate looking for:
below urls format on app user can see
mydomain.com/cat/1 --display cat id 1 |controller=cat, action=displaydetails mydomain.com/dog/2 --display dog id 2 |controller=dog, action=displaydetails mydomain.com/cow/2 --display cow id 3 |controller=cow, action=displaydetails
i have maintained scheme no 2 animals(may of different kind) can have same id, means if there cat id=1, cant have other animal id. scheme can extract animal details+ type animal id
apart existing url pattern, planning create short url in format below
mydomain.com/1 --this show cat mydomain.com/2 --this show dog mydomain.com/3 --this show cow
routes have created below, , appear same order in global.asax
pattern= cat/{id}, controller= cat, action=displaydetails pattern= dog/{id}, controller= dog, action=displaydetails pattern= cow/{id}, controller= cow, action=displaydetails pattern= {id}, controller= displayanyanimal ----------i want help in route
currently controller looks this
public class displayanyanimalcontoller : controller { public actionresult index(string animalid) { //iam processing request here animalid //now know contoller+action needs executed //say instant have display dog id=2 //currently iam doing redirect , working fine, //but changes url ----------------------------------------------- ######################### ### need help here ### ######################### homecoming redirecttoroute(new {contoller="dog",action="displaydetails",id=2 }); ----------------------------------------------- } }
now problem redirecttoroute
/ redirecttoaction
both changes url. dont want alter url pattern.
please suggest me how accomplish this, may suggest exclusively different way, accomplish this
you write custom animal route:
public class animalroute : route { public animalroute(string url, iroutehandler routehandler) : base(url, routehandler) { } public override routedata getroutedata(httpcontextbase httpcontext) { var rd = base.getroutedata(httpcontext); var id = rd.getrequiredstring("id"); // todo: given id decide controller choose: // query database or whatever needed. // code had in origin // of index action of displayanyanimalcontoller // no longer needed. if (id == "1") { rd.values["controller"] = "cat"; } else if (id == "2") { rd.values["controller"] = "dog"; } else if (id == "3") { rd.values["controller"] = "cow"; } else { // no thought animal throw new httpexception(404, "not found"); } rd.values["action"] = "index"; homecoming rd; } }
and register in global.asax
:
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.add(new animalroute("{id}", new mvcroutehandler())); }
now when navigate mydomain.com/1
, getroutedata
method of custom route executed, fetch id=1, , utilize index
action of cat
controller.
asp.net-mvc asp.net-mvc-3 asp.net-routing
Comments
Post a Comment