11using System ;
22using System . IO ;
3+ using AspCoreServer . Data ;
34using Microsoft . AspNetCore . Builder ;
45using Microsoft . AspNetCore . Hosting ;
6+ using Microsoft . AspNetCore . Http ;
57using Microsoft . AspNetCore . SpaServices . Webpack ;
8+ using Microsoft . EntityFrameworkCore ;
69using Microsoft . Extensions . Configuration ;
710using Microsoft . Extensions . DependencyInjection ;
811using Microsoft . Extensions . Logging ;
9- using Microsoft . EntityFrameworkCore ;
10- using AspCoreServer . Data ;
11- using Swashbuckle . AspNetCore . Swagger ;
12- using Microsoft . AspNetCore . Http ;
1312using Microsoft . Net . Http . Headers ;
13+ using Swashbuckle . AspNetCore . Swagger ;
14+ using WebEssentials . AspNetCore . Pwa ;
15+
16+ namespace AspCoreServer {
17+ public class Startup {
18+
19+ public static void Main ( string [ ] args ) {
20+ var host = new WebHostBuilder ( )
21+ . UseKestrel ( )
22+ . UseContentRoot ( Directory . GetCurrentDirectory ( ) )
23+ . UseIISIntegration ( )
24+ . UseStartup < Startup > ( )
25+ . Build ( ) ;
1426
15- namespace AspCoreServer
16- {
17- public class Startup
18- {
19-
20- public static void Main ( string [ ] args )
21- {
22- var host = new WebHostBuilder ( )
23- . UseKestrel ( )
24- . UseContentRoot ( Directory . GetCurrentDirectory ( ) )
25- . UseIISIntegration ( )
26- . UseStartup < Startup > ( )
27- . Build ( ) ;
28-
29- host . Run ( ) ;
27+ host . Run ( ) ;
3028 }
31- public Startup ( IHostingEnvironment env )
32- {
33- var builder = new ConfigurationBuilder ( )
34- . SetBasePath ( env . ContentRootPath )
35- . AddJsonFile ( "appsettings.json" , optional : true , reloadOnChange : true )
36- . AddJsonFile ( $ "appsettings.{ env . EnvironmentName } .json", optional : true )
37- . AddEnvironmentVariables ( ) ;
38- Configuration = builder . Build ( ) ;
29+ public Startup ( IHostingEnvironment env ) {
30+ var builder = new ConfigurationBuilder ( )
31+ . SetBasePath ( env . ContentRootPath )
32+ . AddJsonFile ( "appsettings.json" , optional : true , reloadOnChange : true )
33+ . AddJsonFile ( $ "appsettings.{ env . EnvironmentName } .json", optional : true )
34+ . AddEnvironmentVariables ( ) ;
35+ Configuration = builder . Build ( ) ;
3936 }
4037
4138 public IConfigurationRoot Configuration { get ; }
4239
4340 // This method gets called by the runtime. Use this method to add services to the container.
44- public void ConfigureServices ( IServiceCollection services )
45- {
41+ public void ConfigureServices ( IServiceCollection services ) {
4642 // Add framework services.
47- services . AddMvc ( ) ;
48- services . AddNodeServices ( ) ;
43+ services . AddMvc ( ) ;
44+ services . AddNodeServices ( ) ;
45+ services . AddHttpContextAccessor ( ) ;
46+ services . AddProgressiveWebApp ( new PwaOptions { Strategy = ServiceWorkerStrategy . CacheFirst , RegisterServiceWorker = true , RegisterWebmanifest = true } , "manifest.json" ) ;
4947
5048 var connectionStringBuilder = new Microsoft . Data . Sqlite . SqliteConnectionStringBuilder { DataSource = "spa.db" } ;
51- var connectionString = connectionStringBuilder . ToString ( ) ;
49+ var connectionString = connectionStringBuilder . ToString ( ) ;
5250
53- services . AddDbContext < SpaDbContext > ( options =>
54- options . UseSqlite ( connectionString ) ) ;
51+ services . AddDbContext < SpaDbContext > ( options =>
52+ options . UseSqlite ( connectionString ) ) ;
5553
5654 // Register the Swagger generator, defining one or more Swagger documents
57- services . AddSwaggerGen ( c =>
58- {
59- c . SwaggerDoc ( "v1" , new Info { Title = "Angular 6.0 Universal & ASP.NET Core advanced starter-kit web API" , Version = "v1" } ) ;
55+ services . AddSwaggerGen ( c => {
56+ c . SwaggerDoc ( "v1" , new Info { Title = "Angular 6.0 Universal & ASP.NET Core advanced starter-kit web API" , Version = "v1" } ) ;
6057 } ) ;
6158 }
6259
6360 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
64- public void Configure ( IApplicationBuilder app , IHostingEnvironment env , ILoggerFactory loggerFactory , SpaDbContext context )
65- {
66- loggerFactory . AddConsole ( Configuration . GetSection ( "Logging" ) ) ;
67- loggerFactory . AddDebug ( ) ;
61+ public void Configure ( IApplicationBuilder app , IHostingEnvironment env , ILoggerFactory loggerFactory , SpaDbContext context ) {
62+ loggerFactory . AddConsole ( Configuration . GetSection ( "Logging" ) ) ;
63+ loggerFactory . AddDebug ( ) ;
6864
6965 // app.UseStaticFiles();
7066
71- app . UseStaticFiles ( new StaticFileOptions ( )
72- {
73- OnPrepareResponse = c =>
74- {
67+ app . UseStaticFiles ( new StaticFileOptions ( ) {
68+ OnPrepareResponse = c => {
7569 //Do not add cache to json files. We need to have new versions when we add new translations.
7670
77- if ( ! c . Context . Request . Path . Value . Contains ( ".json" ) )
78- {
79- c . Context . Response . GetTypedHeaders ( ) . CacheControl = new CacheControlHeaderValue ( )
80- {
81- MaxAge = TimeSpan . FromDays ( 30 ) // Cache everything except json for 30 days
71+ if ( ! c . Context . Request . Path . Value . Contains ( ".json" ) ) {
72+ c . Context . Response . GetTypedHeaders ( ) . CacheControl = new CacheControlHeaderValue ( ) {
73+ MaxAge = TimeSpan . FromDays ( 30 ) // Cache everything except json for 30 days
8274 } ;
83- }
84- else
85- {
86- c . Context . Response . GetTypedHeaders ( ) . CacheControl = new CacheControlHeaderValue ( )
87- {
88- MaxAge = TimeSpan . FromMinutes ( 15 ) // Cache json for 15 minutes
75+ } else {
76+ c . Context . Response . GetTypedHeaders ( ) . CacheControl = new CacheControlHeaderValue ( ) {
77+ MaxAge = TimeSpan . FromMinutes ( 15 ) // Cache json for 15 minutes
8978 } ;
9079 }
9180 }
9281 } ) ;
9382
94- DbInitializer . Initialize ( context ) ;
83+ DbInitializer . Initialize ( context ) ;
9584
96- if ( env . IsDevelopment ( ) )
97- {
98- app . UseDeveloperExceptionPage ( ) ;
99- app . UseWebpackDevMiddleware ( new WebpackDevMiddlewareOptions
100- {
85+ if ( env . IsDevelopment ( ) ) {
86+ app . UseDeveloperExceptionPage ( ) ;
87+ app . UseWebpackDevMiddleware ( new WebpackDevMiddlewareOptions {
10188 HotModuleReplacement = true ,
102- HotModuleReplacementEndpoint = "/dist/"
89+ HotModuleReplacementEndpoint = "/dist/"
10390 } ) ;
104- app . UseSwagger ( ) ;
105- app . UseSwaggerUI ( c =>
106- {
107- c . SwaggerEndpoint ( "/swagger/v1/swagger.json" , "My API V1" ) ;
91+ app . UseSwagger ( ) ;
92+ app . UseSwaggerUI ( c => {
93+ c . SwaggerEndpoint ( "/swagger/v1/swagger.json" , "My API V1" ) ;
10894 } ) ;
10995
11096 // Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
11197
112-
113- app . MapWhen ( x => ! x . Request . Path . Value . StartsWith ( "/swagger" , StringComparison . OrdinalIgnoreCase ) , builder =>
114- {
115- builder . UseMvc ( routes =>
116- {
117- routes . MapSpaFallbackRoute (
118- name : "spa-fallback" ,
119- defaults : new { controller = "Home" , action = "Index" } ) ;
98+ app . MapWhen ( x => ! x . Request . Path . Value . StartsWith ( "/swagger" , StringComparison . OrdinalIgnoreCase ) , builder => {
99+ builder . UseMvc ( routes => {
100+ routes . MapSpaFallbackRoute (
101+ name : "spa-fallback" ,
102+ defaults : new { controller = "Home" , action = "Index" } ) ;
120103 } ) ;
121104 } ) ;
122- }
123- else
124- {
125- app . UseMvc ( routes =>
126- {
127- routes . MapRoute (
128- name : "default" ,
129- template : "{controller=Home}/{action=Index}/{id?}" ) ;
130-
131- routes . MapRoute (
132- "Sitemap" ,
133- "sitemap.xml" ,
134- new { controller = "Home" , action = "SitemapXml" } ) ;
135-
136- routes . MapSpaFallbackRoute (
105+ } else {
106+ app . UseMvc ( routes => {
107+ routes . MapRoute (
108+ name : "default" ,
109+ template : "{controller=Home}/{action=Index}/{id?}" ) ;
110+
111+ routes . MapRoute (
112+ "Sitemap" ,
113+ "sitemap.xml" ,
114+ new { controller = "Home" , action = "SitemapXml" } ) ;
115+
116+ routes . MapSpaFallbackRoute (
137117 name : "spa-fallback" ,
138- defaults : new { controller = "Home" , action = "Index" } ) ;
118+ defaults : new { controller = "Home" , action = "Index" } ) ;
139119
140120 } ) ;
141- app . UseExceptionHandler ( "/Home/Error" ) ;
121+ app . UseExceptionHandler ( "/Home/Error" ) ;
142122 }
143123 }
144124 }
145- }
125+ }
0 commit comments