简体   繁体   English

RedirectToRoute如何工作以及如何在其中传递数据?

[英]How does RedirectToRoute work and how to pass data in it?

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. 我正在尝试重定向到RecomController中的路由,并且操作是Index,我已经在RouteConfig文件中定义了路径,并且还指定了在我调用RedirectToRoute函数时传递的id参数。 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. 我还在RecomController Index动作上方创建了Route属性,但它仍然无法将我导航到该路径。 Am i missing something? 我想念什么吗?

RouteConfig.cs: 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: RecomController.cs:

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

Calling the function (IN ANOTHER CONTROLLER): 调用函数(在另一个控制器中):

ProjectController.cs: ProjectController.cs:

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

Sounds like you're using wrong route name or path in RedirectToRoute() , hence it doesn't work: 听起来您在RedirectToRoute()使用了错误的路由名称或路径,因此它不起作用:

// 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 (eg Recomroute ). 因此,您需要提供在RegisterRoutes定义的完整路由名称和路由值(例如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. 1)您仍然需要提供controlleractionid参数以匹配路由定义。

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. 2) Recomroute似乎是在默认路由下定义的,具有相同的路由段定义,它将覆盖其下的所有自定义路由,如果要评估Recomroute请先将其移至最高顺序,然后对默认路由使用不同的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM