至少对于 .net core 5 和 6 来说,这是可能的(使用 VueCLI 时与 VueCliMiddleware 的结果相似)。
您需要做的是关注这篇博文,您需要在 Startup.cs 文件中添加和配置 SpaServices:https://blogs.taiga.nl/martijn/2021/02/24/integrating-vite-with-asp-net-core-a-winning-combination/
我的项目也能正常工作,但我不得不对 npm 脚本进行一些更改,因为其他路由被 SPA 部分劫持:
//Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseResponseCaching();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
if (env.IsDevelopment())
{
app.UseSpa(spa =>
{
// use 'build-dev' npm script
spa.UseReactDevelopmentServer(npmScript: "build-dev");
});
}
}
// package.json
// new script "build-dev" for building vue in development mode
{
"name": "vueapp",
"version": "0.0.0",
"scripts": {
"dev": "echo Starting the development server && vite",
"build": "vite build",
"build-dev": "echo Building for development && vite build --mode development",
"preview": "vite preview",
},
"dependencies": {
"vue": "^3.2.25",
"vue-router": "^4.0.12"
},
"devDependencies": {
"@vitejs/plugin-vue": "^2.0.0",
"eslint": "^8.5.0",
"eslint-plugin-vue": "^8.2.0",
"sass": "^1.45.0",
"vite": "^2.7.2",
"vue-eslint-parser": "^8.0.1"
}
}
您可以从解决方案中启动它,但我更喜欢在终端中使用 dotnet watch run - 您可以为 asp.net 内容热重载,但此设置的缺点是热重载不适用于 Vue。
不管怎样,试一试。