0

I'm trying to redirect to a route that is in the RecomController and the action is Index, i have defined the path in my RouteConfig file and have also specified an id parameter that i pass when i call my RedirectToRoute function. For some reason it can't find that path.

I have also created a Route attribute above the RecomController Index action but it still doesn't navigate me to that path. Am i missing something?

RouteConfig.cs:

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
            name: "Recomroute",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Recom", action = "Index", id = UrlParameter.Optional }
            );
        }

RecomController.cs:

 [Route("Recom/Index")]
        public ActionResult Index(int id)
        {
         ............//Functioning
        }

Calling the function (IN ANOTHER CONTROLLER):

ProjectController.cs:

return RedirectToRoute("Recom/Index/{id}", new {id = projectdto.Id });
Shad
  • 63
  • 1
  • 1
  • 10
  • Regarding your route order, you should look for this issue: https://stackoverflow.com/questions/35661062/why-map-special-routes-first-before-common-routes-in-asp-net-mvc. – Tetsuya Yamamoto Jan 25 '19 at 07:57

1 Answers1

0

Sounds like you're using wrong route name or path in RedirectToRoute(), hence it doesn't work:

// this is wrong because first parameter did not match
return RedirectToRoute("Recom/Index/{id}", new {id = projectdto.Id });

For two parameters overload, it requires route name (not route path) and route values, as shown in definition below:

protected internal RedirectToRouteResult RedirectToRoute(string routeName, 
                           RouteValueDictionary routeValues)

Hence, you need to provide complete route name and route values defined in RegisterRoutes (e.g. Recomroute).

return RedirectToRoute("Recomroute", new {
    controller = "Recom", 
    action = "Index", 
    id = projectdto.Id
});

Side notes:

1) You still need to provide controller, action and id parameters in order to match route definition.

2) The Recomroute seem defined below default route with same route segments definition which overrides all custom routes beneath it, if you want to evaluate Recomroute first move it to the top order and use different path against default route.

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • I have tried this approach, but it still doesn't work, the debug mode for some reason steps over the return RedirectToTRoute function. – Shad Jan 25 '19 at 08:14
  • @Shad What's wrong to use `RedirectToAction()`. What limitation are you facing with this? – TanvirArjel Jan 25 '19 at 08:19
  • The provided syntax is correct; however there's something may went wrong before `RedirectToRoute` kicks in. I preferred using `RedirectToAction("Index", "Recom")` here because your custom route path in `Recomroute` exactly duplicating the default one. – Tetsuya Yamamoto Jan 25 '19 at 08:20
  • The issue with using RedirectToAction is that it uses System.Web.Mvc which interferes with my [HttpPost] request and shows me an error, plus im also using IHttpActionResult to return other things in my api controller – Shad Jan 25 '19 at 08:43
  • Please provide the error message you've got, that sounds like different problem than `RedirectToResult` usage. Is that `ProjectController` contains `[HttpPost]` marked action method which redirects to `/Recom/Index`? – Tetsuya Yamamoto Jan 25 '19 at 08:46
  • That's not the issue as the error was 'an unbigious reference between System.Web.Mvc and System.Web.Http for HttpPost' I had fixed it by specifying System.Web.Http.HttpPost, the issue is that you can't use a RedirectToAction in an api controller as it only exists in a normal controller. – Shad Jan 25 '19 at 09:05
  • I fixed the reference to `System.Web.Http.RedirectToRoute`, therefore `RedirectAction` cannot be used here because I assumed `ProjectController` is a Web API controller, not MVC one. The `routeValues` here is a plain object which should contain controller name, action name and parameters to access the controller action. – Tetsuya Yamamoto Jan 25 '19 at 09:31