11using System ;
22using System . Collections . Generic ;
3- using System . IO ;
43using System . Linq ;
54using System . Threading . Tasks ;
65using Microsoft . AspNetCore . Builder ;
76using Microsoft . AspNetCore . Hosting ;
8- using Microsoft . AspNetCore . Http ;
97using Microsoft . AspNetCore . SpaServices . Webpack ;
8+ using Microsoft . Extensions . Configuration ;
109using Microsoft . Extensions . DependencyInjection ;
1110using Microsoft . Extensions . Logging ;
12- using Newtonsoft . Json . Serialization ;
1311
1412namespace WebApplicationBasic
1513{
1614 public class Startup
1715 {
16+ public Startup ( IHostingEnvironment env )
17+ {
18+ var builder = new ConfigurationBuilder ( )
19+ . SetBasePath ( env . ContentRootPath )
20+ . AddJsonFile ( "appsettings.json" , optional : true , reloadOnChange : true )
21+ . AddJsonFile ( $ "appsettings.{ env . EnvironmentName } .json", optional : true )
22+ . AddEnvironmentVariables ( ) ;
23+ Configuration = builder . Build ( ) ;
24+ }
25+
26+ public IConfigurationRoot Configuration { get ; }
27+
1828 // This method gets called by the runtime. Use this method to add services to the container.
1929 public void ConfigureServices ( IServiceCollection services )
2030 {
21- services . AddMvc ( ) . AddJsonOptions ( options =>
22- {
23- options . SerializerSettings . ContractResolver = new CamelCasePropertyNamesContractResolver ( ) ;
24- } ) ;
31+ // Add framework services.
32+ services . AddMvc ( ) ;
2533 }
2634
2735 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
28- public void Configure ( IApplicationBuilder app , ILoggerFactory loggerFactory , IHostingEnvironment env )
36+ public void Configure ( IApplicationBuilder app , IHostingEnvironment env , ILoggerFactory loggerFactory )
2937 {
30- app . UseDeveloperExceptionPage ( ) ;
38+ loggerFactory . AddConsole ( Configuration . GetSection ( "Logging" ) ) ;
39+ loggerFactory . AddDebug ( ) ;
3140
32- if ( env . IsDevelopment ( ) ) {
41+ if ( env . IsDevelopment ( ) )
42+ {
43+ app . UseDeveloperExceptionPage ( ) ;
3344 app . UseWebpackDevMiddleware ( new WebpackDevMiddlewareOptions {
3445 HotModuleReplacement = true
3546 } ) ;
3647 }
48+ else
49+ {
50+ app . UseExceptionHandler ( "/Home/Error" ) ;
51+ }
3752
3853 app . UseStaticFiles ( ) ;
39- loggerFactory . AddConsole ( ) ;
54+
4055 app . UseMvc ( routes =>
4156 {
4257 routes . MapRoute (
@@ -48,17 +63,5 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, IHo
4863 defaults : new { controller = "Home" , action = "Index" } ) ;
4964 } ) ;
5065 }
51-
52- public static void Main ( string [ ] args )
53- {
54- var host = new WebHostBuilder ( )
55- . UseContentRoot ( Directory . GetCurrentDirectory ( ) )
56- . UseIISIntegration ( )
57- . UseKestrel ( )
58- . UseStartup < Startup > ( )
59- . Build ( ) ;
60-
61- host . Run ( ) ;
62- }
6366 }
6467}
0 commit comments