Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
[HttpGet]
       public IActionResult justdate()
       {
           var date = DateTime.Now.Date.ToShortDateString;
           return Ok(date);
       }


Error:
System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported.
   at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter`1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)


What I have tried:

Please help with code snippets or links for this issue.
Posted
Updated 17-Feb-22 14:38pm

If you're using .NET 6, you can use the new DateOnly type:
DateOnly Struct (System) | Microsoft Docs[^]
C#
[HttpGet]
public IActionResult justdate()
{
    var date = DateOnly.FromDateTime(DateTime.Today);
    return Ok(date);
}
 
Share this answer
 
That code won't even compile, much less generate a run time error:
C#
var date = DateTime.Now.Date.ToShortDateString;
Will give you an error:
Cannot assign method group to an implicitly-typed variable
You need to start by correcting that - add the brackets to call the method:
C#
var date = DateTime.Now.Date.ToShortDateString();

Now your code will maybe compile - and you won't be running the old version of the code ...
 
Share this answer
 
Comments
Richard Deeming 29-Nov-21 4:41am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900