我正在使用针对dotnet核心的Angular SPA项目模板(VS2017和dotnet core 2.0中可用的项目模板)进行项目。
如果用户转到无效的网址,我有一个Angular组件可以显示“Not Found”消息。
我还启用了服务器端预渲染。从服务器返回预呈现的“Not Found”页面时,如何让它返回404状态代码?
我发现这样做的每个方法都使用Express作为后端网络服务器,我无法在aspnet核心后端找到任何资源。
感谢您的帮助!
为清晰起见编辑:
我不打算为特定的MVC控制器操作或应用程序错误返回404。
我希望从特定的Angular2 / 4组件返回404,由Microsoft.AspNetCore.SpaServices呈现服务器端。
为了进行比较,here是使用NodeJS上的Express Web服务器和服务器端呈现的角度通用解决方案的示例。
答案 0 :(得分:2)
我找到了解决方案。
首先,我们需要创建一个可注入的服务,组件可以使用它来设置状态代码:
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;
}
}
我们需要将此服务添加到主providers
模块中的AppModule
数组中:
...
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);
}
}