C# HTTP侦听器显示多条消息问题
在我的项目(.net core 2.2+angular 8)中,为了显示一个错误HttpInterceptor,它工作得非常完美, 但对于多条消息,它也可以工作,但不会显示正确的错误消息。 我得到了这样的东西:C# HTTP侦听器显示多条消息问题,c#,angular,http,asp.net-core,C#,Angular,Http,Asp.net Core,在我的项目(.net core 2.2+angular 8)中,为了显示一个错误HttpInterceptor,它工作得非常完美, 但对于多条消息,它也可以工作,但不会显示正确的错误消息。 我得到了这样的东西: [object Object] One or more validation errors occurred. 400 0HLRCTBS664E8:00000002 我的拦截器看起来像: @Injectable() export class ErrorInterceptor imple
[object Object] One or more validation errors occurred. 400 0HLRCTBS664E8:00000002
我的拦截器看起来像:
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
catchError(error => {
if (error instanceof HttpErrorResponse) {
if (error.status === 401) {
return throwError(error.statusText);
}
const applicationError = error.headers.get('Application-Error');
if (applicationError) {
console.error(applicationError);
return throwError(applicationError);
}
const serverError = error.error;
let modalStateErrors = '';
if (serverError && typeof serverError === 'object') {
for (const key in serverError) {
if (serverError[key]) {
modalStateErrors += serverError[key] + '\n';
}
}
}
return throwError(modalStateErrors || serverError || 'Server Error');
}
})
)
}
}
export const ErrorInterceptorProvide = {
provide: HTTP_INTERCEPTORS,
useClass: ErrorInterceptor,
multi: true
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(builder =>
{
builder.Run(async context =>
{
context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
var error = context.Features.Get<IExceptionHandlerFeature>();
if (error != null)
{
context.Response.AddApplicationError(error.Error.Message);
await context.Response.WriteAsync(error.Error.Message);
}
});
});
}
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseAuthentication();
app.UseMvc();
}
}
我的Startup类看起来像:
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
catchError(error => {
if (error instanceof HttpErrorResponse) {
if (error.status === 401) {
return throwError(error.statusText);
}
const applicationError = error.headers.get('Application-Error');
if (applicationError) {
console.error(applicationError);
return throwError(applicationError);
}
const serverError = error.error;
let modalStateErrors = '';
if (serverError && typeof serverError === 'object') {
for (const key in serverError) {
if (serverError[key]) {
modalStateErrors += serverError[key] + '\n';
}
}
}
return throwError(modalStateErrors || serverError || 'Server Error');
}
})
)
}
}
export const ErrorInterceptorProvide = {
provide: HTTP_INTERCEPTORS,
useClass: ErrorInterceptor,
multi: true
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(builder =>
{
builder.Run(async context =>
{
context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
var error = context.Features.Get<IExceptionHandlerFeature>();
if (error != null)
{
context.Response.AddApplicationError(error.Error.Message);
await context.Response.WriteAsync(error.Error.Message);
}
});
});
}
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseAuthentication();
app.UseMvc();
}
}
似乎您的API中存在问题 避免此错误的最佳方法是抑制此错误 要禁用自动400行为,请设置 SuppressModelStateInvalidFilter属性设置为true。添加以下内容 Startup.ConfigureServices中突出显示的代码: 这是本文中包含的代码:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressConsumesConstraintForFormFileParameters = true;
options.SuppressInferBindingSourcesForParameters = true;
options.SuppressModelStateInvalidFilter = true;
options.SuppressMapClientErrors = true;
options.ClientErrorMapping[404].Link =
"https://httpstatuses.com/404";
});
microsoft似乎您的API存在问题 避免此错误的最佳方法是抑制此错误 要禁用自动400行为,请设置 SuppressModelStateInvalidFilter属性设置为true。添加以下内容 Startup.ConfigureServices中突出显示的代码: 这是本文中包含的代码:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressConsumesConstraintForFormFileParameters = true;
options.SuppressInferBindingSourcesForParameters = true;
options.SuppressModelStateInvalidFilter = true;
options.SuppressMapClientErrors = true;
options.ClientErrorMapping[404].Link =
"https://httpstatuses.com/404";
});
我试过微软的但不起作用。有趣的是,我试过显示一条信息,但它不起作用。有趣的是,它只显示一条信息