开发者

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

开发者 https://www.devze.com 2023-03-15 19:03 出处:网络
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.

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.

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. 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:

       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.


Not quite.

You can create a wildcard parameter by mapping {*params}.
This will give you a single string containing all of the parameters, which you can then .Split('/').


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消