【问题标题】:Setting up an ASP Net Core app with subdirectories设置带有子目录的 ASP Net Core 应用程序
【发布时间】:2018-11-11 19:10:12
【问题描述】:

我正在尝试在子目录 (http://<website>/app) 中设置一个 ASP Net Core 应用程序,但我遇到了一个问题,即应用程序无法识别它的 URL 基础应该以 /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;
    }
}

这两个应用程序现在似乎都在发出正确的请求。

【问题讨论】:

  • 应用请求http://&lt;website&gt;/&lt;static_content&gt;是什么意思?您是否引用了视图中的静态文件,例如` `

标签: c# nginx asp.net-core


【解决方案1】:

对于从//app 的响应请求,请在Startup.cs 中尝试以下代码

            app.Map("/app",
            subApp =>
            {
                subApp.UseStaticFiles();
                subApp.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });

            }
        );

【讨论】:

  • 我通过删除我的 NGINX 配置中的最后一个斜杠让它工作了,不过我很感激!
【解决方案2】:

Configure 中的核心 3.1 确保您已设置 app.UsePathBase("/api or whatever subdir");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多