我找到了解決方案。
首先,我們需要創建一個可注射的服務組件可以用來設置狀態代碼:
import { Injectable } from '@angular/core';
@Injectable()
export class HttpStatusCodeService {
private statusCode: number;
constructor(){
this.statusCode = 200;
}
public setStatusCode(statusCode: number) {
this.statusCode = statusCode;
}
public getStatusCode(): number {
return this.statusCode;
}
}
我們需要這種服務的主要AppModule
模塊添加到providers
陣列:
...
providers: [
...
HttpStatusCodeService,
...
]
...
然後我們需要把我們的boot.server.ts
文件中添加兩行(加上import語句)(注意,這是基於VS2017模板創建的庫存文件):
import 'reflect-metadata';
import 'zone.js';
import 'rxjs/add/operator/first';
import { APP_BASE_HREF } from '@angular/common';
import { enableProdMode, ApplicationRef, NgZone, ValueProvider } from '@angular/core';
import { platformDynamicServer, PlatformState, INITIAL_CONFIG } from '@angular/platform-server';
import { createServerRenderer, RenderResult } from 'aspnet-prerendering';
import { AppModule } from './app/app.module.server';
//ADD THIS LINE
import { HttpStatusCodeService } from './path/to/services/http-status-code.service';
enableProdMode();
export default createServerRenderer(params => {
const providers = [
{ provide: INITIAL_CONFIG, useValue: { document: '<app></app>', url: params.url } },
{ provide: APP_BASE_HREF, useValue: params.baseUrl },
{ provide: 'BASE_URL', useValue: params.origin + params.baseUrl },
];
return platformDynamicServer(providers).bootstrapModule(AppModule).then(moduleRef => {
const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
const state = moduleRef.injector.get(PlatformState);
const zone = moduleRef.injector.get(NgZone);
//ADD THIS LINE: this will get the instance of the HttpStatusCodeService created for this request.
const statusCodeService = moduleRef.injector.get(HttpStatusCodeService);
return new Promise<RenderResult>((resolve, reject) => {
zone.onError.subscribe((errorInfo: any) => reject(errorInfo));
appRef.isStable.first(isStable => isStable).subscribe(() => {
// Because 'onStable' fires before 'onError', we have to delay slightly before
// completing the request in case there's an error to report
setImmediate(() => {
resolve({
html: state.renderToString(),
//ADD THIS LINE: this will get the currently set status code and return it along with the prerendered html string
statusCode: statusCodeService.getStatusCode()
});
moduleRef.destroy();
});
});
});
});
});
然後終於,我們需要設置任何組件的狀態代碼,不應該返回HTTP 200:
import { Component } from '@angular/core';
import { HttpStatusCodeService } from './path/to/services/http-status-code.service';
@Component({
selector: 'not-found',
templateUrl: './not-found.html'
})
export class NotFoundComponent {
constructor(private httpStatusCodeService: HttpStatusCodeService) {
httpStatusCodeService.setStatusCode(404);
}
}
我認爲這將有助於你https://stackoverflow.com/questions/667053/best-way-to-implement -a-404-in-asp-net – sheplu
更好https://stackoverflow.com/questions/37690114/asp-net-core-how-to-return-a-specific-status-代碼和內容不受控制 – Eliseo
感謝您的建議,但這些都不能解決我的問題。他們都在討論從標準的ASP.Net/ASPNet Core應用程序返回404響應,而不是服務器端呈現的Angular組件。爲了清晰我編輯了我的問題。 – Entith