【问题标题】:use custom route with RedirectToAction (asp.net mvc)使用带有 RedirectToAction 的自定义路由(asp.net mvc)
【发布时间】:2011-03-08 10:38:03
【问题描述】:

我在操作方法中使用带参数的 RedirectToAction

return RedirectToAction("ListThreads", "View", new { category = "1", subcategory = "49"});

重定向后的网址是这样的 http://domain/Forum/View/ListThreads?category=1&subcategory=49

我希望它像这样生成 http://domain/Forum/View/ListThreads/1/49

怎么做?

注意:我在 global.asax 中已经有一个路由,所有页面/链接都在使用它。

context.MapRoute(
            "Forum_subcategory",
            "Forum/{controller}/{action}/{category}/{subcategory}",
            new { controller = "View", action = "ListThreads", category = "", subcategory = "" }
        );

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-routing


    【解决方案1】:

    我在应用程序中遇到了类似的路由问题,为了解决我使用的问题:RedirectToRoute。如果您将 RedirectToAction 更改为:

    return RedirectToRoute("Forum_subcategory", new { controller = "View", action = "ListThreads", category = "1", subcategory = "49" });
    

    你应该得到你正在寻找的结果。更多Controller.RedirectToRoute方法参考:http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoroute(v=vs.98).aspx

    【讨论】:

    • 不适合我:(
    【解决方案2】:

    我以前见过类似的问题。听起来很垃圾,您可能必须将对象包装在 RouteValueDictionary 中:

    return RedirectToAction("ListThreads", "View", new RouteValueDictionary(new { category = "1", subcategory = "49"}));
    

    试一试,看看效果如何。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-02
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      相关资源
      最近更新 更多