Last active
October 16, 2017 06:36
-
-
Save Vladimir-Novick/b69cf6d2f76f1d639190b176301acafb to your computer and use it in GitHub Desktop.
Add Json Options to AspNetCore2 Application, Configuration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.AspNetCore.Http; | |
using System; | |
using Microsoft.AspNetCore.ResponseCompression; | |
using System.IO.Compression; | |
using Newtonsoft.Json.Serialization; | |
using System.Runtime; | |
using Newtonsoft.Json.Converters; | |
using Newtonsoft.Json; | |
namespace memoryDB | |
{ | |
public class Startup | |
{ | |
public Startup(IHostingEnvironment env) | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(env.ContentRootPath) | |
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); | |
Configuration = builder.Build(); | |
} | |
public IConfigurationRoot Configuration { get; } | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvcCore(). | |
AddDataAnnotations(). | |
AddJsonFormatters(); | |
services.Configure<GzipCompressionProviderOptions> | |
(options => options.Level = CompressionLevel.Fastest); | |
services.AddResponseCompression(options => | |
{ | |
options.Providers.Add<GzipCompressionProvider>(); | |
}); | |
services.AddMvc((options) => | |
{ | |
options.CacheProfiles.Add("default", new CacheProfile() | |
{ | |
Duration = 0, | |
Location = ResponseCacheLocation.None | |
}); | |
}).AddJsonOptions( | |
o => | |
{ | |
CamelCasePropertyNamesContractResolver resorver = new CamelCasePropertyNamesContractResolver(); | |
resorver.NamingStrategy = null; | |
o.SerializerSettings.ContractResolver = resorver; | |
o.SerializerSettings.Converters.Add(new StringEnumConverter()); | |
o.SerializerSettings.Formatting = Formatting.None; | |
o.SerializerSettings.NullValueHandling = NullValueHandling.Include; | |
o.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore; | |
o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Error; | |
}); | |
} | |
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) | |
{ | |
loggerFactory.AddConsole(Configuration.GetSection("Logging")); | |
loggerFactory.AddConsole(); | |
var logger = loggerFactory.CreateLogger("Start"); | |
logger.LogInformation($"- Application started "); | |
string strGName = ""; | |
if (GCSettings.IsServerGC == true) | |
strGName = "server"; | |
else | |
strGName = "workstation"; | |
logger.LogInformation($"The {strGName} garbage collector is running."); | |
app.UseResponseCompression(); | |
app.UseStaticFiles(); | |
app.UseMvc(routes => | |
{ | |
routes.MapRoute( | |
name: "default", | |
template: "{controller=Home}/{action=Index}/{id?}"); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment