asp.net-mvc – ASP.NET WEB API将DateTime作为URI的一部分传递
发布时间:2020-12-16 06:47:34 所属栏目:asp.Net 来源:网络整理
导读:假设我有一个使用以下方法的Controller: public int Get(DateTime date){ // return count from a repository based on the date} 我希望能够在将日期作为URI本身的一部分传递时访问方法,但是目前我只能在将日期作为查询字符串传递时才能使用它.例如: Get/
假设我有一个使用以下方法的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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – DDR Treeview菜单显示选定的根及其子节点
- .net – 将数据写入App_Data
- asp.net-mvc – RavenDB Ids和ASP.NET MVC3路由
- ASP.NET MVC如何实现自定义验证(服务端验证+客户端验证)
- asp.net-core – 如何在Entity Framework Core中运行存储过
- asp.net-core – JsonConvert DataSet .NET Core 2.0无效
- 处理ASP.Net MVC中的过期标题
- ASP.Net MVC身份无限重定向循环
- asp.net-mvc – 如何构建这个ASP.NET n层解决方案?
- asp.net – 实体框架与存储过程
推荐文章
站长推荐
- ASP.Net MVC Cookies不会持续存在
- asp.net – 在非生产环境中复制与负载相关的崩溃
- asp.net-mvc – 带密码的.NET核心分布式Redis缓存
- asp.net-mvc – Html.BeginForm()与GET方法
- asp.net-web-api – .NET WebApi查看原始请求
- asp.net-mvc-3 – ASP.NET MVC 3中ModelState.Is
- 如何在ASP.NET Core 1.0 RC2中加载程序集
- asp.net-mvc – 在带有Web Essentials的Visual S
- 使用ASP.NET MVC进行Windows身份验证
- asp.net – RPC_E_SERVERFAULT
热点阅读