简体   繁体   中英

Date column in SQL Server to System.DateTime in ASP.NET model

I have a date_creation_user column in my SQL Server database of date type. I use Entity Framework (database-first) so in the User model class I have :

public System.DateTime date_creation_user { get; set; }

I created my Web API with POST and GET methods.

For POST I enter data :

{
    //
    "Date_creation_user": "2019-04-12"
}

With Get, the result is :

{
    "Client_ID": 1,
    "Date_creation_user": "2012-02-24T00:00:00",
}

I want to get a clear DATE format because I need a GET method, to get all users with a date of creation, how can I implement that method ?

Here are my two GET methods and my POST :

GET all :

[HttpGet]
//[Authorize]
public async Task<HttpResponseMessage> Get()
{
    List<ClientModel> model = new List<ClientModel>();

    try
    {
        clService.GetClients().ToList().ForEach(a =>
        {
            ClientModel compts = new ClientModel
            {
                Client_ID = a.Client_ID,
                Type_Client = a.Type_Client,
                Date_creation_user = a.Date_creation_client

            };

            model.Add(compts);

        });
        return Request.CreateResponse(HttpStatusCode.OK, model);
    }
    catch (Exception ex)
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest, ex);
    }
}

GET with ID :

[HttpGet]
public HttpResponseMessage find(int id)
{
    try
    {
        var result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new StringContent(JsonConvert.SerializeObject(clService.GetClient(id)));
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        return result;
    } 
    catch
    {
        return new HttpResponseMessage(HttpStatusCode.BadRequest);
    }
}

POST :

[HttpPost]
public HttpResponseMessage CreateClient(CLIENT Client)
{
    try
    {
        var result = clService.AjoutClient(Client);
        return Request.CreateResponse(HttpStatusCode.Created, result);
    }
    catch (Exception ex)
    {
        return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
    }
}

How can I implement that GET method with date parameter ?

Thanks

更改此行:

Date_creation_user = a.Date_creation_client.ToString("yyyy-MM-dd")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM