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

发布于 2024-11-17 16:24:12 字数 782 浏览 5 评论 0原文

我希望用户能够使用 Restful 语法访问对象(可以是 JSON 或 XML),而不必使用查询字符串。

因此,他们可以做类似 http://mywebsite.com/objects/ 的事情,而不是 http://mywebsite.com/objects/get=obj1&get=obj2&get=someotherobject/ obj1/obj2/ 并将返回 xml/JSON。他们可以按任何顺序列出对象,就像使用查询字符串一样。

在 asp.net mvc 中,您可以像这样映射路由:

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

我想做类似的事情:

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

其中 params 数组将包含每个 get。

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.

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

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

发布评论

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

评论(2

稚然 2024-11-24 16:24:12

不完全是。

您可以通过映射 {*params} 创建通配符参数。
这将为您提供一个包含所有参数的字符串,然后您可以.Split('/')

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('/').

月竹挽风 2024-11-24 16:24:12

您可以使用包罗万象的参数

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

,这会将参数作为字符串传递,您可以在 / 上拆分该字符串以获取数组。

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.

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