将自定义路由与 RedirectToAction (asp.net mvc) 一起使用

发布于 2024-10-20 14:11:55 字数 787 浏览 4 评论 0 原文

我在操作方法中使用带参数的 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 = "" }
        );

I use the RedirectToAction with parameters in my action method

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

After redirect the url comes out like this
http://domain/Forum/View/ListThreads?category=1&subcategory=49

I want it to be generated like
http://domain/Forum/View/ListThreads/1/49

How to do it?

Note : I already have a route in global.asax that is used by all the pages/links.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独难免 2024-10-27 14:11:55

我在应用程序中遇到了类似的路由问题,为了解决我的问题,我使用了: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

I had a simular problem with routing in my application, to fix my problem I used: RedirectToRoute. If you change your RedirectToAction to this:

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

You should get the result you are looking for. For more information the Controller.RedirectToRoute Method refer to: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoroute(v=vs.98).aspx

守不住的情 2024-10-27 14:11:55

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

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

尝试一下,看看效果如何。

I've seen similar issues before. As crap as it sounds, you might have to wrap your object in a RouteValueDictionary:

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

Give it a try, see how it goes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文