Working with date time in web api

感情迁移 提交于 2019-12-24 18:49:25

问题


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);
            }
        }

The WebApiConfig file includes

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}$" }
       );

After that I am passing the URL like http://localhost:14909/api/meters/GetByMsn/000029000033/2017-10-06T07:52:27

The error I am getting is 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

Update 1

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

Any help would be highly appreciated.


回答1:


After a lot of searching I finally found a solution.

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



来源:https://stackoverflow.com/questions/46660261/working-with-date-time-in-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!