crazy novice
crazy novice

Reputation: 1817

Redirection to different controller method

I need to redirect a request to another controller. So basically I have www.MyDomain.com/SomeParameter. I need to read this parameter value from URL and then redirect to another controller for further processing. For this purpose I have two controllers in my project Home and Employee as follows.

public class HomeController : Controller
    {
        public ActionResult Index(string ID)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                string EmployeeAccountNumber = GetEmployeeNumberFromID(ID);
                RedirectToAction("Employee", "Index", EmployeeAccountNumber);
            }

            return View();
        }

        private string GetEmployeeNumberFromID(string ID)
        {
            return "Alpha";
        }
    }

public class EmployeeController : Controller
    {
        //
        // GET: /Employee/

        public ActionResult Index(string id)
        {
            return View();
        }

    }

I then added Routing is defined as follows in Global.asax

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

            routes.MapRoute(
                "Default", // Route name
                "{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );

            routes.MapRoute(
                "EmployeeManagement", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Employee", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );
        }

As you can see in the Index method of HomeController class, I want to Redirect to my EmployeeController, however, the breakpoint I set in EmployeeController.Index never get hit. Am I doing something wrong here?

Upvotes: 0

Views: 341

Answers (3)

Satpal
Satpal

Reputation: 133403

Use

return RedirectToAction("Employee", "Index", new {"id"=EmployeeAccountNumber});

instead of

RedirectToAction("Employee", "Index", EmployeeAccountNumber);

You are not passing route parameters

Upvotes: 1

harshal
harshal

Reputation: 592

public class HomeController : Controller
        {
        public ActionResult Index(int id)
        {
// Redirect/Index/id is because of route defined as {controller}/{action}/{id}
//if u need the url to be say "www.MyDomain.com/3240" then change route definition as
//"{id}" instead of "{controller}/{action}/{id}". Tested and working
           return RedirectToAction("Index", "Redirect/Index/" + id);
        }
}

public class RedirectController : Controller
    {
        //
        // GET: /Redirect/

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

    }

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }    // Parameter defaults
            );

            routes.MapRoute(
               "redirectedroute", // Route name
               "{controller}/{action}/{id}", // URL with parameters
               new { controller = "RedirectController", action = "Index", id = UrlParameter.Optional } // Parameter defaults
           );

Upvotes: 1

harshal
harshal

Reputation: 592

Instead of RedirectToAction() try Redirect("/Employee") and let controller handle rest

Upvotes: 0

Related Questions