i want to know how to search datetime in url of web api, i want to search the date and it can show with xml format in website. such as if i search 2018-01-01 and it will show after 2018-01-01 date and equals to 2018-01-01. do i need to parse datetime to string first? Is this possible to do it?
my code:
[HttpGet()]
public IHttpActionResult Get()
{
List<TestClass> test = new List<TestClass>();
string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "SELECT UserID, Name, Mobile, Access, Date From tbluser";
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
SqlDataReader reader = sqlcomm.ExecuteReader();
while (reader.Read())
{
Test.Add(new TestClass()
{
UserId = Convert.ToInt32(reader.GetValue(0)),
Name = reader.GetValue(1).ToString(),
Mobile = reader.GetValue(2).ToString(),
Age = Convert.ToInt32(reader.GetValue(3)),
Date = Convert.ToDateTime(reader.GetValue(4))
});
}
return Ok(test);
}
my class:
[DataContract]
public class TestClass
{
[DataMember(Order=1)]
public string UserId { get; set; }
[DataMember(Order = 2)]
public string Name { get; set; }
[DataMember(Order = 3)]
public string Mobile { get; set; }
[DataMember(Order = 4)]
public int Age { get; set; }
[DataMember(Order = 5)]
public DateTime Date { get; set; }
}
expected:
http://localhost:IP/api/value?Date=2018-01-01
expected result:
<ArrayOfTest xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Test>
<UserId>1</UserId>
<Name>Emily</Name>
<Mobile>8375269</Mobile>
<Age>21</Age>
<Date>2018-01-01</Date>
</Test>
<Test>
<UserId>2</UserId>
<Name>Victor</Name>
<Mobile>08314732</Mobile>
<Age>31</Age>
<Date>2022-03-05</Date>
</Test>
<Test>
<UserId>3</UserId>
<Name>Peter</Name>
<Mobile>32475962</Mobile>
<Age>45</Age>
<Date>2019-10-25</Date>
</Test>
<Test>
<UserId>4</UserId>
<Name>John</Name>
<Mobile>24398592</Mobile>
<Age>24</Age>
<Date>2018-02-16</Date>
</Test>
<Test>
<UserId>5</UserId>
<Name>Kobe</Name>
<Mobile>19873491</Mobile>
<Age>38</Age>
<Date>2020-05-12</Date>
</Test>