编辑:我设法使用有效证书通过 IIS 运行此程序,但我还没有找到放弃新代理方法的方法。如果您想了解有关使用 IIS 而不是 IIS Express 的一些信息,请跳到结尾。
简而言之:使用最新的 ASP.NET 6 和 Angular 模板 (Visual Studio 2022),如何绕过反向代理并使用与以前版本的 .NET 相同的方法?我不想通过 SPA URL 运行应用程序,并将后端请求转发到 SPA 代理。
详细来说,我的目标:
我想导航到 https://mydomain.local 并让它为所有内容提供服务,这在 .NET 6 之前没有问题。您可以使用
UseProxyToSpaDevelopmentServer
,运行 npm start
,一切都很好。
虽然我知道您仍然可以使用它,但 .NET 6 放弃了
Startup.cs
,并且指南建议继续使用简化的 Program.cs
。
我已经尝试过:
aspnetcore-https.js
)proxy.conf.js
中删除 proxyConfig 属性来摆脱代理配置 (
angular.json
angular.json
似乎没有任何效果,文档也不是很好。
此刻:
SpaProxyServerUrl
属性定义的值任何帮助将不胜感激。
我不确定自签名 ASP.NET 证书有什么问题。我只是删除了它的所有引用并再次添加它们。
%AppData%\Roaming\ASP.NET\https
inetcpl.cpl
)chrome://net-internals/#hsts
chrome://settings/clearBrowserData
)mmc
文件 --> 添加/删除管理单元 --> 证书)。删除个人证书和受信任的根证书dotnet dev-certs https --trust
要放弃 IIS Express 并回复普通 IIS 实例:
mkcert
创建有效的自签名证书。launchSettings.json
launchSettings.json
,随时删除任何不需要的配置文件。你想要类似这样的东西:{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iis": {
"applicationUrl": "https://yourdomain.local",
"sslPort": 443
}
},
"profiles": {
"IIS": {
"commandName": "IIS",
"launchBrowser": true,
"launchUrl": "https://yourdomain.local",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
}
}
}
}
angular.json
以相应地引用它。const PROXY_CONFIG = [
{
context: [
"/yourRoute",
],
target: "https://yourdomain.local:443",
secure: false,
changeOrigin: true,
logLevel: "info",
headers: {
Connection: 'Keep-Alive'
}
}
]
module.exports = PROXY_CONFIG;
.csproj
文件并确保 <SpaProxyServerUrl></SpaProxyServerUrl>
中定义的端口与使用的端口匹配 angular.json
就是这样,你就可以开始了。手动启动 Angular 应用程序,访问
https://yourdomain.local
,一切都应该很清楚。
我所做的就是将以下内容添加到我的 package.json 中,这样我就可以手动启动 Angular 并且不需要在每次服务器端更改时重新启动。
{
...
"scripts": {
"dev": "set ASPNETCORE_HTTPS_PORT=7186 && npm start"
}
...
}
将
7186
更改为 .Net 6 服务器运行的端口(在您的情况下为 443)。然后运行“npm run dev”,等待它加载并启动后端服务器。
在 Linux 上运行 Angular .Net 项目时我也遇到了这个问题,我为我们的项目修复了这个问题,我想运行 IIS 时也会出现同样的问题。
Browser -> Angular dev server tries serving -> Rest to .Net server
直接从 Angular 运行开发服务器,没有代理,什么都没有。优点是它可以正常工作,缺点是 image/jpeg 和 text/html 请求不通过 .Net 中间件。
通过.Net启动SPA
Browser -> .Net server -> proxy-conf.js -> Angular dev server or .Net based on conf
或者如果您在本地运行项目
Browser -> Angular dev server -> proxy-conf.js -> .Net server
Browser -> .Net server handles -> Server static files if needed
// proxy-conf.js
const { env } = require('process');
// replace 44373 with your .Net server https port. If not using SSL then you could use the http port I think.
const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'http://localhost:44373';
const PROXY_CONFIG = [
{
context: [
// Add the urls you want to go to the server, double star is nice
// "/api/*" matches "/api/controller" and
// "/api/**" matches "/api/controller/some/method/id"
"/images/**",
"/files/**",
"/api/**"
],
target: target,
// This defaults to false, but absolutely needed to have the same behavior
// as running the SPA through the .Net server.
followRedirects: true,
secure: false,
headers: {
Connection: 'Keep-Alive'
}
}
]
module.exports = PROXY_CONFIG;