When i uploaded my razor pages application to server always getting this error 'The value '14/09/2022' is not valid for ...*.' i could figure out the issue,the format is interpreted as mm/dd/yyyy. i could not change the system dateformat for my server.
So what i did is i changed my applications Culture and UI-Culture Via IIS manager.
Even after that changes,its still showing the same issue.Im using Bootstrap datepicker. Any help would be appreciated.Thanks.
Edited
Startup.cs
public class CustomDateConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Debug.Assert(typeToConvert == typeof(DateTime));
return DateTime.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToUniversalTime().ToString("dd'/'mm'/'yyyy"));
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddDbContext<ApplicationDbContext>(options =>
options.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddDebug()))
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddRazorPages().AddJsonOptions(options =>{
options.JsonSerializerOptions.Converters.Add(new CustomDateConverter());
});
services.AddMemoryCache();
}