使用基于 ASP.NET MVC 属性的路由映射器
在我的 ASP.NET MVC 应用程序中,我想使用 这个 ASP.NET MVC 基于属性的路由映射器,首次公布 在这里。
到目前为止,我了解如何实现使用它的代码,但我遇到了一些问题,我认为那些过去使用过这种基于属性的路由映射器的人将能够回答这些问题。
- 如何将它与用于
HTTP POST
的 ActionResults 一起使用? 换句话说,它如何与表单提交等一起使用?我应该只放入GET
方法的 URL,还是应该使用不带任何参数的GET
方法 URL(如HTTP POST
中所示)不是通过 URL 传入的)? - 如何将其与“URL 查询字符串参数”一起使用? 是否可以将该属性配置为映射到
/controller/action?id=value
等路由,而不是/controller/action/{id}
?
提前致谢。
In my ASP.NET MVC application, I want to use this ASP.NET MVC Attribute Based Route Mapper, first announced here.
So far, I understand how to implement code that uses it, but I've run into a few questions that I think those who have used this attribute-based route mapper in the past will be able to answer.
- How do I use it with ActionResults that are for
HTTP POST
s? In other words, how does it work with form submissions and the like? Should I just put the URL of theGET
method in, or should I use theGET
method URL without any parameters (as inHTTP POST
they aren't passed in through the URL)? - How do I use it with "URL querystring parameters"? Can the attribute be configured to map to a route such as
/controller/action?id=value
rather than/controller/action/{id}
?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
[HttpPost]
属性来装饰要发布到的操作:如果您决定为 POST 操作指定不同的名称:
您需要在辅助方法中提供此名称:
查询字符串参数不是路由定义的一部分。您始终可以在控制器操作中作为操作参数或从
Request.Params
获取它们。只要
id 参数涉及它在
Application_Start
中配置,因此如果您希望它出现在查询字符串中而不是成为路由的一部分,只需将其从该路由定义中删除即可:You decorate the action that you are posting to with the
[HttpPost]
attribute:If you decide to give the POST action a different name:
You need to supply this name in your helper methods:
Query string parameters are not part of the route definition. You can always obtain them in a controller action either as action parameter or from
Request.Params
.As far as the
id
parameter is concerned it is configured inApplication_Start
, so if you want it to appear in the query string instead of being part of the route simply remove it from this route definition: