代码改变世界

Controller中利用RedirectToRoute重定向的问题

2016-04-22 11:15  无抿屎的牛牛  阅读(6431)  评论(0编辑  收藏  举报

先看看我们例子用到的路由表

 routes.MapRouteWithName(
                name: "ProductDetail",
                url: "{Platform}/Product/{ProductID}",
                defaults: new { controller = "Home", action = "Product"}
               );
  • 方法1
protected internal RedirectToRouteResult RedirectToRoute(
    Object routeValues
)

eg:

 return RedirectToRoute(new
    {
         controller = "Home", //控制器
         action = "Product", //Action
         ProductID = 1, //参数
         nickName = "wahaha" //参数
    });
  • 方法2
protected internal RedirectToRouteResult RedirectToRoute(
    string routeName,
    Object routeValues
)

eg:

重定向到该路由

return RedirectToRoute("ProductDetail", new { Platform = "WeChat", ProductID = 1 });
  • 方法3
protected internal RedirectToRouteResult RedirectToRoute(
    RouteValueDictionary routeValues
)

eg:

 return RedirectToRoute(new System.Web.Routing.RouteValueDictionary(new { 
                action= "Product",
                controller = "Home",  
                ProductId =1 //参数
            }));
  • 方法4
protected internal virtual RedirectToRouteResult RedirectToRoute(
    string routeName,
    RouteValueDictionary routeValues
)

eg:
return RedirectToRoute("ProductDetail", new System.Web.Routing.RouteValueDictionary(new
{
    Platform = "WeChat",
    ProductID = 1

}));