| 
13 | 13 | 
 
  | 
14 | 14 | using Microsoft.AspNetCore.NodeServices;  | 
15 | 15 | using AspCoreServer.Data;  | 
 | 16 | +using Swashbuckle.AspNetCore.Swagger;  | 
16 | 17 | 
 
  | 
17 | 18 | namespace AspCoreServer  | 
18 | 19 | {  | 
19 |  | -    public class Startup  | 
 | 20 | +  public class Startup  | 
 | 21 | +  {  | 
 | 22 | + | 
 | 23 | +    public static void Main(string[] args)  | 
 | 24 | +    {  | 
 | 25 | +      var host = new WebHostBuilder()  | 
 | 26 | +          .UseKestrel()  | 
 | 27 | +          .UseContentRoot(Directory.GetCurrentDirectory())  | 
 | 28 | +          .UseIISIntegration()  | 
 | 29 | +          .UseStartup<Startup>()  | 
 | 30 | +          .Build();  | 
 | 31 | + | 
 | 32 | +      host.Run();  | 
 | 33 | +    }  | 
 | 34 | +    public Startup(IHostingEnvironment env)  | 
 | 35 | +    {  | 
 | 36 | +      var builder = new ConfigurationBuilder()  | 
 | 37 | +          .SetBasePath(env.ContentRootPath)  | 
 | 38 | +          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)  | 
 | 39 | +          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)  | 
 | 40 | +          .AddEnvironmentVariables();  | 
 | 41 | +      Configuration = builder.Build();  | 
 | 42 | +    }  | 
 | 43 | + | 
 | 44 | +    public IConfigurationRoot Configuration { get; }  | 
 | 45 | + | 
 | 46 | +    // This method gets called by the runtime. Use this method to add services to the container.  | 
 | 47 | +    public void ConfigureServices(IServiceCollection services)  | 
 | 48 | +    {  | 
 | 49 | +      // Add framework services.  | 
 | 50 | +      services.AddMvc();  | 
 | 51 | +      services.AddNodeServices();  | 
 | 52 | + | 
 | 53 | +      var connectionStringBuilder = new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder { DataSource = "spa.db" };  | 
 | 54 | +      var connectionString = connectionStringBuilder.ToString();  | 
 | 55 | + | 
 | 56 | +      services.AddDbContext<SpaDbContext>(options =>  | 
 | 57 | +          options.UseSqlite(connectionString));  | 
 | 58 | + | 
 | 59 | +      // Register the Swagger generator, defining one or more Swagger documents  | 
 | 60 | +      services.AddSwaggerGen(c =>  | 
 | 61 | +      {  | 
 | 62 | +        c.SwaggerDoc("v1", new Info { Title = "Angular 4.0 Universal & ASP.NET Core advanced starter-kit web API", Version = "v1" });  | 
 | 63 | +      });  | 
 | 64 | +    }  | 
 | 65 | + | 
 | 66 | +    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  | 
 | 67 | +    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, SpaDbContext context)  | 
20 | 68 |     {  | 
 | 69 | +      loggerFactory.AddConsole(Configuration.GetSection("Logging"));  | 
 | 70 | +      loggerFactory.AddDebug();  | 
 | 71 | + | 
 | 72 | +      if (env.IsDevelopment())  | 
 | 73 | +      {  | 
 | 74 | +        app.UseDeveloperExceptionPage();  | 
 | 75 | +        app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {  | 
 | 76 | +          HotModuleReplacement = true  | 
 | 77 | +        });  | 
21 | 78 | 
 
  | 
22 |  | -        public static void Main(string[] args)  | 
 | 79 | +        DbInitializer.Initialize(context);  | 
 | 80 | + | 
 | 81 | +        app.UseSwagger();  | 
 | 82 | + | 
 | 83 | +        // Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.  | 
 | 84 | +        app.UseSwaggerUI(c =>  | 
23 | 85 |         {  | 
24 |  | -            var host = new WebHostBuilder()  | 
25 |  | -                .UseKestrel()  | 
26 |  | -                .UseContentRoot(Directory.GetCurrentDirectory())  | 
27 |  | -                .UseIISIntegration()  | 
28 |  | -                .UseStartup<Startup>()  | 
29 |  | -                .Build();  | 
30 |  | - | 
31 |  | -            host.Run();  | 
32 |  | -        }  | 
33 |  | -        public Startup(IHostingEnvironment env)  | 
 | 86 | +          c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");  | 
 | 87 | +        });  | 
 | 88 | + | 
 | 89 | +        app.MapWhen(x => !x.Request.Path.Value.StartsWith("/swagger"), builder =>  | 
34 | 90 |         {  | 
35 |  | -            var builder = new ConfigurationBuilder()  | 
36 |  | -                .SetBasePath(env.ContentRootPath)  | 
37 |  | -                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)  | 
38 |  | -                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)  | 
39 |  | -                .AddEnvironmentVariables();  | 
40 |  | -            Configuration = builder.Build();  | 
41 |  | -        }  | 
42 |  | - | 
43 |  | -        public IConfigurationRoot Configuration { get; }  | 
44 |  | - | 
45 |  | -        // This method gets called by the runtime. Use this method to add services to the container.  | 
46 |  | -        public void ConfigureServices(IServiceCollection services)  | 
 | 91 | +          builder.UseMvc(routes =>  | 
 | 92 | +          {  | 
 | 93 | +            routes.MapSpaFallbackRoute(  | 
 | 94 | +                      name: "spa-fallback",  | 
 | 95 | +                      defaults: new { controller = "Home", action = "Index" });  | 
 | 96 | +          });  | 
 | 97 | +        });  | 
 | 98 | +      }  | 
 | 99 | +      else  | 
 | 100 | +      {  | 
 | 101 | +        app.UseMvc(routes =>  | 
47 | 102 |         {  | 
48 |  | -            // Add framework services.  | 
49 |  | -            services.AddMvc();  | 
50 |  | -            services.AddNodeServices();  | 
 | 103 | +          routes.MapRoute(  | 
 | 104 | +              name: "default",  | 
 | 105 | +              template: "{controller=Home}/{action=Index}/{id?}");  | 
51 | 106 | 
 
  | 
52 |  | -            var connectionStringBuilder = new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder { DataSource = "spa.db" };  | 
53 |  | -            var connectionString = connectionStringBuilder.ToString();  | 
 | 107 | +          routes.MapSpaFallbackRoute(  | 
 | 108 | +              name: "spa-fallback",  | 
 | 109 | +              defaults: new { controller = "Home", action = "Index" });  | 
 | 110 | +        });  | 
 | 111 | +        app.UseExceptionHandler("/Home/Error");  | 
 | 112 | +      }  | 
54 | 113 | 
 
  | 
55 |  | -            services.AddDbContext<SpaDbContext>(options =>  | 
56 |  | -                options.UseSqlite(connectionString));  | 
57 |  | -        }  | 
58 |  | - | 
59 |  | -        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  | 
60 |  | -        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, SpaDbContext context)  | 
61 |  | -        {  | 
62 |  | -            loggerFactory.AddConsole(Configuration.GetSection("Logging"));  | 
63 |  | -            loggerFactory.AddDebug();  | 
64 |  | - | 
65 |  | -            if (env.IsDevelopment())  | 
66 |  | -            {  | 
67 |  | -                app.UseDeveloperExceptionPage();  | 
68 |  | -                app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {  | 
69 |  | -                    HotModuleReplacement = true  | 
70 |  | -                });  | 
71 |  | - | 
72 |  | -                DbInitializer.Initialize(context);  | 
73 |  | -            }  | 
74 |  | -            else  | 
75 |  | -            {  | 
76 |  | -                app.UseExceptionHandler("/Home/Error");  | 
77 |  | -            }  | 
78 |  | - | 
79 |  | -            app.UseStaticFiles();  | 
80 |  | - | 
81 |  | -            app.UseMvc(routes =>  | 
82 |  | -            {  | 
83 |  | -                routes.MapRoute(  | 
84 |  | -                    name: "default",  | 
85 |  | -                    template: "{controller=Home}/{action=Index}/{id?}");  | 
86 |  | - | 
87 |  | -                routes.MapSpaFallbackRoute(  | 
88 |  | -                    name: "spa-fallback",  | 
89 |  | -                    defaults: new { controller = "Home", action = "Index" });  | 
90 |  | -            });  | 
91 |  | -        }  | 
 | 114 | +      app.UseStaticFiles();  | 
92 | 115 |     }  | 
 | 116 | +  }  | 
93 | 117 | }  | 
0 commit comments