是否可以使用maproute在asp.net mvc中路由多个参数
我希望用户能够使用 Restful 语法访问对象(可以是 JSON 或 XML),而不必使用查询字符串。
因此,他们可以做类似 http://mywebsite.com/objects/ 的事情,而不是
并将返回 xml/JSON。他们可以按任何顺序列出对象,就像使用查询字符串一样。http://mywebsite.com/objects/get=obj1&get=obj2&get=someotherobject/
obj1/obj2/
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不完全是。
您可以通过映射
{*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('/')
.您可以使用包罗万象的参数
,这会将参数作为字符串传递,您可以在
/
上拆分该字符串以获取数组。You could use a catchall parameter
This would pass params as a string that you could split on a
/
to get an array.