Redirect to another controller + action without changing the URL in ASP.Net MVC3

Note Below is a small demo sort to simulate what I'm looking for:

Below is the URL format of my application that the user can see

mydomain.com/cat/1 --display cat with id 1 |controller=Cat, action=DisplayDetails mydomain.com/dog/2 --display dog with id 2 |controller=Dog, action=DisplayDetails mydomain.com/cow/2 --display cow with id 3 |controller=Cow, action=DisplayDetails 

I supported a system in which none of the two animals (maybe of a different type) can have the same identifier, which means that if there is a cat with id = 1, we do not have another animal with this identifier. Also from my system I can retrieve animal details + type only from animal id

In addition to the existing URL pattern, I plan to create a short URL in the format below

 mydomain.com/1 --this will show cat mydomain.com/2 --this will show dog mydomain.com/3 --this will show cow 

The routes I created are below and they look the same 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 this Route 

Currently, the controller is as follows

 public class DisplayAnyAnimalContoller : Controller { public ActionResult Index(string animalId) { //iam processing request here from animalId //now i know which contoller+action needs to be executed //say for instant i have to display dog with id=2 //currently iam doing this to redirect and its working fine, //but it changes url ----------------------------------------------- ######################### ### i need help here ### ######################### return RedirectToRoute(new {contoller="Dog",action="DisplayDetails",id=2 }); ----------------------------------------------- } } 

Now the problem with RedirectToRoute / RedirectToAction is that they both change the URL. But I do not want to change my url template.

Please suggest me how to achieve this, you can offer a completely different way to achieve this

+6
source share
1 answer

You can write your own pet 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 the id decide which controller to choose: // you could query the database or whatever it is needed. // Basically that will be the code you had in the beginning // of the index action of your DisplayAnyAnimalContoller which // is 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 idea what this animal was throw new HttpException(404, "Not found"); } rd.Values["action"] = "index"; return rd; } } 

and then register it in Global.asax :

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.Add(new AnimalRoute("{id}", new MvcRouteHandler())); } 

Now, when you go to mydomain.com/1 , the GetRouteData method of the custom route will be executed, id = 1 will be retrieved, and then it will use the Index action of the Cat controller.

+6
source

Source: https://habr.com/ru/post/892433/


All Articles