在Web API中使用日期时间 [英] Working with date time in web api

查看:193
本文介绍了在Web API中使用日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用MySQL数据库创建了WEB API.该表包含有关仪表序列号,信号强度值以及信号强度出现的日期时间的数据.目前,我已成功发送仪表的序列号来获取数据.

I have created a WEB APIusing MySQL database. The table includes the data about the meter's serial numbers it's signal strength values and the date time on which the signal strength has comes. For now i am successful in getting data by sending the meter's serial number.

现在我也向它发送了日期时间参数.但这对我不起作用.以下是我所做的.

Now I am also sending the date time parameter to it also. But it's not working for me. Below is what i have done.

public HttpResponseMessage GetByMsn(string msn, DateTime dt)
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.Found, medEntitites.tj_xhqd.Where(m=> m.msn == msn).Where(a=> a.date_time < dt ));
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
        }

WebApiConfig文件包含

config.Routes.MapHttpRoute(
       name: "GetByMsn",
       routeTemplate: "api/{controller}/{action}/{msn}/{dt}",
       defaults: null,
       constraints: new { msn = @"^[0-9]+$" , dt = @"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$" }
       );

此后,我像http://localhost:14909/api/meters/GetByMsn/000029000033/2017-10-06T07:52:27

我遇到的错误是A potentially dangerous Request.Path value was detected from the client (:).

为此,我也搜索了解决方案,但找不到正确的解决方案.我也用日期时间替换了:,但仍然对我不起作用

For this I have also searched for the solutions but couldn't find out the correct one. Also I have replaced : in date time but still it's not working for me

更新1

虽然仅传递日期2017-10-06,但它对我有用,但是当我附加时间时,它不起作用

While passing only date2017-10-06 it works for me but when I append it with time it doesn't works

为了快速检查,我检查了这个

For a quick check I checked this question but still i am unable to solve the solution.

我一定想念我不知道的东西

I must be missing something that I don't know

任何帮助将不胜感激.

推荐答案

经过大量搜索,我终于找到了解决方案.

After a lot of searching I finally found a solution.

我在Web.config中更改了预定义的禁止/无效字符.在下方,添加了以下内容:<httpRuntime requestPathInvalidCharacters="&lt;,&gt;,%,&amp;,*,\,?" />.我从无效字符的标准列表中删除了:.

I have changed the predefined disallowed/invalid characters in my Web.config. Under , added the following: <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,%,&amp;,*,\,?" />. I've removed the : from the standard list of invalid characters.

有关更多信息,请参见此解决方案

For more information see this solution

这篇关于在Web API中使用日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆