Optimus
Optimus

Reputation: 2210

How to override a route in MVC 5

I've two controller controllerOne is defined under an area AreaOne . I need to reroute one of the action defined in the controllerOne to the action defined in controllerTwo I tried attribute routing but its not working. my current code is given below

Action In Controller One

 public class ControllerOne
    {   public ActionResult CustomerSearch()
    {

                return View("Search", model);
    }
}

Action In Controller Two

[Route("AreaOne/ControllerOne/CustomerSearch")]
     public class ControllerTwo
        {   public ActionResult CustomCustomerSearch()
        {

                    return View("Search", model);
        }
    }

How can I achieve this.?

Upvotes: 2

Views: 3434

Answers (1)

AMZ
AMZ

Reputation: 540

In RouteConfig.cs you can use routes.MapRoute to associate your URL with any controller & action in your project.

            routes.MapRoute(
                name: "OverrideCustomerSearch",
                url: "AreaOne/ControllerOne/CustomerSearch",
                defaults: new { controller = "Two", action = "CustomCustomerSearch" }
        );

Upvotes: 2

Related Questions