加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读