我的 Angular 项目在与我的 API 后端 (asp.net) 不同的端口上运行。我正在使用代理将所有 http 请求路由到后端网络服务器,如下所示。
{
"/api": {
"target": {
"host": "localhost",
"protocol": "https:",
"port": 5001
},
"secure": false,
"logLevel" : "debug",
"changeOrigin": true
}
}
我的 Angular 项目服务选项是,
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "art-ng-core:build",
"host": "www.artngcore.com",
"port": 8500,
"sslKey": "key.key",
"sslCert": "server.crt"
},
"configurations": {
"production": {
"browserTarget": "art-ng-core:build:production"
}
}
},
我的问题是 API 提供的静态文件,即图像。
<img src={{imagePath}}>
我的后端静态文件在不同的静态文件夹“ SystemData ”中提供,共享路径为“ Data ”。
假设我有一个带有相对路径的文件:/Data/UserData/test.jpg
. 如果我提供src
具有相对路径的属性,则图像的 URL 将看起来像https://www.artngcore.com:8500/Data/UserData/test.jpg
它应该在的位置https://localhost:5001/Data/UserData/test.jpg
如何以与 Http 调用相同的方式代理对静态文件的调用?
现在,我通过让我的 api 提供完整 URL 而不是相对路径来解决这个问题,但我不确定在处理静态文件时这是否是一种常见或好的做法。
我正在我的数据库中保存相对路径,并运行以下方法来生成 URL
public string GenerateUrl(string filePath, HttpRequest ctx) {
string physicalPath = this.GetRootPath() + filePath;
var url = physicalPath.Replace(Directory.GetCurrentDirectory (),$@"{ctx.Scheme}://{ctx.Host}").Replace(@"SystemData", @"Data");
return url;
}
谢谢你。
您希望以与以 开头"/Data"
的URL完全相同的方式处理 URL "/api"
。
所以...只是做同样的事情,并在您的代理配置 dor 中添加一个部分"/Data"
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句