Skip to content

Instantly share code, notes, and snippets.

@poychang
Forked from regisdiogo/Startup.cs
Last active August 8, 2018 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poychang/35489bc7cfd46f4cd02258d9938f0591 to your computer and use it in GitHub Desktop.
Save poychang/35489bc7cfd46f4cd02258d9938f0591 to your computer and use it in GitHub Desktop.
[設定 ASP.NET Core 可以序列化 Enum]- 修改 MVC 的 Json 序列化設定,使之可以將 Enum 序列化成文字併忽略 null 值 #dotnet #csharp
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment