简体   繁体   English

是否可以使用 maproute 在 asp.net mvc 中路由多个参数

[英]Is it possible to have route multiple parameters in asp.net mvc using maproute

I want a user to be able to access objects (could be JSON or XML) using a restful syntax rather than having to use query strings.我希望用户能够使用 restful 语法访问对象(可能是 JSON 或 XML),而不必使用查询字符串。

So instead of http://mywebsite.com/objects/get=obj1&get=obj2&get=someotherobject/ they could do something like http://mywebsite.com/objects/obj1/obj2/ and the xml/JSON would be returned.因此,代替http://mywebsite.com/objects/get=obj1&get=obj2&get=someotherobject/他们可以执行类似http://mywebsite.com/objects/obj1/obj2/之类的操作,并且将返回 xml/JSON。 They could list the objects in any order just like you can with query strings.他们可以按任何顺序列出对象,就像使用查询字符串一样。

In asp.net mvc you map a route like so:在 asp.net mvc 你 map 像这样的路线:

       routes.MapRoute(
           "MyRoute",
           "MyController/MyAction/{param}",
           new { controller = "MyController", action = "MyAction", param = "" }
       );

I would want to do something like:我想做类似的事情:

       routes.MapRoute(
           "MyRoute",
           "MyController/MyAction/{params}",
           new { controller = "MyController", action = "MyAction", params = [] }
       );

where the params array would contain each get.其中params数组将包含每个 get。

You could use a catchall parameter你可以使用一个包罗万象的参数

   routes.MapRoute(
       "MyRoute",
       "MyController/MyAction/{*params}",
       new { controller = "MyController", action = "MyAction"}
   );

This would pass params as a string that you could split on a / to get an array.这会将参数作为字符串传递,您可以在/上拆分该字符串以获取数组。

Not quite.不完全的。

You can create a wildcard parameter by mapping {*params} .您可以通过映射{*params}创建通配符参数。
This will give you a single string containing all of the parameters, which you can then .Split('/') .这将为您提供一个包含所有参数的字符串,然后您可以.Split('/')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM