ASP MVC - 根据查询参数调用不同的操作处理程序?

发布于 2024-08-21 03:52:54 字数 280 浏览 2 评论 0原文

我希望根据查询参数值采取不同的操作来处理请求。

例如:

mydomain.com/controller/action?version

=1&msg= hello 和

mydomain.com/controller/action?version=2&msg=5

应根据版本值转到不同的处理程序。

必需/可选的查询参数列表及其类型可能会更改 - 在 version=1 中,msg 是一个字符串,在 version=2 中它是一个整数

I would like different a different action to handle a request depending on query parameters values.

For example:

mydomain.com/controller/action?version=1&msg=hello

and

mydomain.com/controller/action?version=2&msg=5

should go to a different handlers based on the version value.

The list of query params required/optional, as well as their types might change - in version=1, msg is a string, in version=2 it is an integer

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

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

发布评论

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

评论(1

冬天旳寂寞 2024-08-28 03:52:54

您可以使用路由约束

routes.MapRoute("first", "/controller/action/{version}/{msg}", 
    new {controller = "controller", action = "action", 
        version = String.Empty, msg = String.Empty},
    new {version = "1"});

routes.MapRoute("first", "/controller/action/{version}/{msg}", 
    new {controller = "controller", action = "action2", 
        version = String.Empty, msg = String.Empty},
    new {version = "2"});

You could use Route Constraints:

routes.MapRoute("first", "/controller/action/{version}/{msg}", 
    new {controller = "controller", action = "action", 
        version = String.Empty, msg = String.Empty},
    new {version = "1"});

routes.MapRoute("first", "/controller/action/{version}/{msg}", 
    new {controller = "controller", action = "action2", 
        version = String.Empty, msg = String.Empty},
    new {version = "2"});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文