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

时间:2010-02-10 15:25:33

标签: asp.net-mvc model-view-controller action

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

例如:

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

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

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

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

1 个答案:

答案 0 :(得分:2)

您可以使用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"});