1
1
using Microsoft . AspNetCore . Builder ;
2
2
using Microsoft . AspNetCore . Hosting ;
3
- using Microsoft . AspNetCore . Mvc ;
3
+ using Microsoft . AspNetCore . SpaServices ;
4
4
using Microsoft . Extensions . Configuration ;
5
5
using Microsoft . Extensions . DependencyInjection ;
6
+ using Microsoft . Extensions . Hosting ;
6
7
using VueCliMiddleware ;
7
8
8
9
namespace AspNetCoreVueStarter
@@ -19,17 +20,17 @@ public Startup(IConfiguration configuration)
19
20
// This method gets called by the runtime. Use this method to add services to the container.
20
21
public void ConfigureServices ( IServiceCollection services )
21
22
{
22
- services . AddMvc ( ) . SetCompatibilityVersion ( CompatibilityVersion . Version_2_1 ) ;
23
+ services . AddControllersWithViews ( ) ;
23
24
24
- // In production, the React files will be served from this directory
25
+ // In production, the Vue files will be served from this directory
25
26
services . AddSpaStaticFiles ( configuration =>
26
27
{
27
28
configuration . RootPath = "ClientApp/dist" ;
28
29
} ) ;
29
30
}
30
31
31
32
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
32
- public void Configure ( IApplicationBuilder app , IHostingEnvironment env )
33
+ public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
33
34
{
34
35
if ( env . IsDevelopment ( ) )
35
36
{
@@ -38,33 +39,44 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
38
39
else
39
40
{
40
41
app . UseExceptionHandler ( "/Error" ) ;
42
+ // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
41
43
app . UseHsts ( ) ;
42
44
app . UseHttpsRedirection ( ) ;
43
45
}
44
46
45
47
app . UseStaticFiles ( ) ;
46
48
app . UseSpaStaticFiles ( ) ;
47
49
48
- app . UseMvc ( routes =>
50
+ app . UseRouting ( ) ;
51
+
52
+ app . UseEndpoints ( endpoints =>
49
53
{
50
- routes . MapRoute (
54
+ endpoints . MapControllerRoute (
51
55
name : "default" ,
52
- template : "{controller}/{action=Index}/{id?}" ) ;
56
+ pattern : "{controller}/{action=Index}/{id?}" ) ;
57
+
58
+ #if DEBUG
59
+ if ( System . Diagnostics . Debugger . IsAttached )
60
+ endpoints . MapToVueCliProxy ( "{*path}" , new SpaOptions { SourcePath = "ClientApp" } , "serve" , regex : "Compiled successfully" ) ;
61
+ else
62
+ #endif
63
+ // note: output of vue cli or quasar cli should be wwwroot
64
+ endpoints . MapFallbackToFile ( "index.html" ) ;
53
65
} ) ;
54
66
55
- app . UseSpa ( spa =>
56
- {
57
- spa . Options . SourcePath = "ClientApp" ;
67
+ // app.UseSpa(spa =>
68
+ // {
69
+ // spa.Options.SourcePath = "ClientApp";
58
70
59
- if ( env . IsDevelopment ( ) )
60
- {
61
- // run npm process with client app
62
- spa . UseVueCli ( npmScript : "serve" , port : 8080 ) ;
63
- // if you just prefer to proxy requests from client app, use proxy to SPA dev server instead:
64
- // app should be already running before starting a .NET client
65
- // spa.UseProxyToSpaDevelopmentServer("http://localhost:8080"); // your Vue app port
66
- }
67
- } ) ;
71
+ // if (env.IsDevelopment())
72
+ // {
73
+ // // run npm process with client app
74
+ // spa.UseVueCli(npmScript: "serve", port: 8080);
75
+ // // if you just prefer to proxy requests from client app, use proxy to SPA dev server instead:
76
+ // // app should be already running before starting a .NET client
77
+ // // spa.UseProxyToSpaDevelopmentServer("http://localhost:8080"); // your Vue app port
78
+ // }
79
+ // });
68
80
}
69
81
}
70
82
}
0 commit comments