假设我有一个使用以下方法的Controller:
public int Get(DateTime date) { // return count from a repository based on the date }
我希望能够在将日期作为URI本身的一部分传递时访问方法,但是目前我只能在将日期作为查询字符串传递时才能使用它.例如:
Get/2012-06-21T16%3A49%3A54-05%3A00 // does not work Get?date=2005-11-13%205%3A30%3A00 // works
任何想法我怎么能让这个工作?我已尝试使用自定义MediaTypeFormatters,但即使我将它们添加到HttpConfiguration的Formatters列表中,它们似乎也从未被执行过.
解决方法
让我们看看你的默认MVC路由代码:
routes.MapRoute( "Default","{controller}/{action}/{id}",new {controller = "Home",action = "Index",**id** = UrlParameter.Optional} );
好的.看到名称ID?您需要将方法参数命名为“id”,以便模型绑定器知道您要绑定到它.
用这个 –
public int Get(DateTime id)// Whatever id value I get try to serialize it to datetime type. { //If I Couldn't specify a normalized NET datetime object,then set id param to null. // return count from a repository based on the date }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。