c# - 设置带有子目录的 ASP Net Core 应用程序
问题描述
http://<website>/app
我正在尝试在子目录(因此,当我向静态内容或操作发出请求时,它的行为就好像基础是“/”而不是“/app”。(例如:http://<website>/app/<static_content>
是我需要的,但应用程序要求http://<website>/<static_content>
)
NGINX 是这样设置的:
server {
listen 80 default_server;
server_name <IP_Address>;
location /app/ {
proxy_pass http://localhost:4000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}}
我的程序.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseWebRoot(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls("http://localhost:4000");
}
我的 startup.cs 包含以下内容:
app.UsePathBase("/app");
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),
RequestPath = "/app/wwwroot"
});
编辑: 我通过删除proxy_pass上的结尾斜杠来让它工作!
server {
listen 80 default_server;
server_name <IP_Address>;
location /app1/ {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /app2/ {
proxy_pass http://localhost:5020;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
这两个应用程序现在似乎都在提出正确的请求。
解决方案
要响应来自/
to的请求/app
,请尝试以下代码Startup.cs
app.Map("/app",
subApp =>
{
subApp.UseStaticFiles();
subApp.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
);
推荐阅读
- pyspark - 有点复杂的 PySpark 选择和聚合查询
- ios - 有没有办法从 Realm Cloud 中的实例中获取用户?
- r - 在函数中正确使用 str_replace_all
- c# - 如何使用 blazor 创建 cookie 客户端
- c# - 使用 C# 从 s3 存储桶下载文件
- c# - 如何在其控制器中强制转换 XAML 项?[C#],[Xamarin.Forms]
- string - 自动将单个字符从某个位置移动到另一个位置
- python - 如何保存调整大小的图形?
- listview - Xamarin.Forms ListView - 刷新有时会暂时隐藏图像
- c - 评估 Return-Address 的偏移量