在 Angular 4 应用程序中工作。创建了用于向 api 发送请求的 http 拦截器。
@Injectable()
export class HttpRequestInteceptor implements HttpInterceptor {
loginUserName = "";
constructor(private router: ActivatedRoute) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (environment.Authentication == 1) {
// add a custom header
const newRequest = req.clone({headers: req.headers.set('userName', '11111')});
return next.handle(newRequest);
}
return next.handle(req);
}
}
我已经在我的 app.module.ts 中配置了这个拦截器,如下所示
providers: {
provide: HTTP_INTERCEPTORS,
useClass: HttpRequestInteceptor,
multi: true
}
在这里,我试图在 get 请求中传递一个自定义标头值,但是当我查看 chrome 中的网络选项卡时,我没有看到我的标头值在请求中传递。
我的 Asp.Net Web api 代码可能是。
HttpApplication app = (HttpApplication)sender;
string user = app.Context.Request.Headers.GetValues("userName").First();
当我从邮递员和高级重置客户端访问 url 时,我在 web.api 代码中获取用户名值。
但是当我从 Angular 4 应用程序访问相同的 api url 时,我没有得到标题值。
请在我失踪的地方帮助我。