我有一个 .NET Core 2.1 网站,Angular 7 位于 /ClientApp 中。
我正在使用 Microsoft.AspNetCore.SpaServices.Extensions
如果我转到一个页面,例如 localhost:5001/pagedoesnotexist/index,那么由于某种原因,Angular 应用程序会启动。
问题 - 如果我转到不存在的页面,如何更改配置以使 ASP.NET Core 不会尝试重定向到 Angular 应用程序?
我希望它在不正确的网络请求时简单地返回标准 404。我想要一个特定的页面,例如 /admin/dashboard 只为 Angular 应用程序提供服务
启动.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
...
}
公共无效配置(IApplicationBuilder 应用程序,IHostingEnvironment env)
{
...
app.UseSpaStaticFiles();
app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
...
}
虽然这是我的第一个 Angular 应用程序,但到目前为止它似乎运行良好,问题出在开发环境中。
谢谢。
更新 1
进一步澄清一点:
通过:我想要 localhost:5001/ = 404
PASS:我希望 localhost:5001/angtastic/ 为应用程序提供服务
失败:我从 zone.js (socksjs-node/info) 得到重复的 info?t=[random numbers] (它可以在没有下面针对子文件夹的代码的情况下工作,所以也许我做错了什么)。我的猜测是套接字或与 webpack 相关的东西(在 angular cli 的引擎盖下)在处理此子文件夹中的应用程序时遇到了麻烦。
我有一个带有问题的部分解决方案:
app.Map(new PathString("/angtastic"), angtastic =>
{
angtastic.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start --app=angtastic --base-href=/angtastic/ --serve-path=/angtastic/ ");
}
});
});
此外,将 index.html 更改为 BaseURL 为 .
这是我的 Angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"angtastic": {
"root": "src",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/angtastic",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "angtastic:build"
},
"configurations": {
"production": {
"browserTarget": "angtastic:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "angtastic:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"clientapp/src/styles.scss"
],
"scripts": [],
"assets": [
"clientapp/src/favicon.ico",
"clientapp/src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"angtastic-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "angtastic:serve"
},
"configurations": {
"production": {
"devServerTarget": "angtastic:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "angtastic"
}