|
| 1 | +using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.AspNetCore.Hosting; |
| 3 | +using Microsoft.AspNetCore.SpaServices; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Hosting; |
| 7 | +using VueCliMiddleware; |
| 8 | + |
| 9 | +namespace NetCoreWebApiWithVue |
| 10 | +{ |
| 11 | + public class Startup |
| 12 | + { |
| 13 | + public Startup(IConfiguration configuration) |
| 14 | + { |
| 15 | + Configuration = configuration; |
| 16 | + } |
| 17 | + |
| 18 | + public IConfiguration Configuration { get; } |
| 19 | + |
| 20 | + // This method gets called by the runtime. Use this method to add services to the container. |
| 21 | + public void ConfigureServices(IServiceCollection services) |
| 22 | + { |
| 23 | + // NOTE: PRODUCTION Ensure this is the same path that is specified in your webpack output |
| 24 | + services.AddSpaStaticFiles(opt => opt.RootPath = "ClientApp/dist"); |
| 25 | + services.AddControllers(); |
| 26 | + } |
| 27 | + |
| 28 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| 29 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
| 30 | + { |
| 31 | + if (env.IsDevelopment()) |
| 32 | + { |
| 33 | + app.UseDeveloperExceptionPage(); |
| 34 | + } |
| 35 | + |
| 36 | + // NOTE: this is optional, it adds HTTPS to Kestrel |
| 37 | + app.UseHttpsRedirection(); |
| 38 | + |
| 39 | + // NOTE: PRODUCTION uses webpack static files |
| 40 | + app.UseSpaStaticFiles(); |
| 41 | + |
| 42 | + app.UseRouting(); |
| 43 | + |
| 44 | + app.UseAuthorization(); |
| 45 | + |
| 46 | + app.UseEndpoints(endpoints => |
| 47 | + { |
| 48 | + endpoints.MapControllers(); |
| 49 | + |
| 50 | + // NOTE: VueCliProxy is meant for developement and hot module reload |
| 51 | + // NOTE: SSR has not been tested |
| 52 | + // Production systems should only need the UseSpaStaticFiles() (above) |
| 53 | + // You could wrap this proxy in either |
| 54 | + // if (System.Diagnostics.Debugger.IsAttached) |
| 55 | + // or a preprocessor such as #if DEBUG |
| 56 | + endpoints.MapToVueCliProxy( |
| 57 | + "{*path}", |
| 58 | + new SpaOptions { SourcePath = "ClientApp" }, |
| 59 | + npmScript: (System.Diagnostics.Debugger.IsAttached) ? "serve" : null, |
| 60 | + regex: "Compiled successfully", |
| 61 | + forceKill: true |
| 62 | + ); |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments