【问题标题】:How to create urls for .NET Core application in Apache virtual directory如何在 Apache 虚拟目录中为 .NET Core 应用程序创建 url
【发布时间】:2021-03-24 00:03:23
【问题描述】:

ASP.NET Core 应用程序是使用 Visual Studion 2019 ASP.NET Core 应用程序项目模板创建的。

其 _layout.cshtml 包含相对于应用程序根目录的 css 文件路径:

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - WebApplication1</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>

此应用程序在 Debian Linux 10 中运行,并使用来自 https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache 的指令在 nettest 虚拟目录中使用 apache

Apache mod_proxy 和标头模块是用户将 https 请求转发到 kestrel 服务器。 Apache 配置文件:

<Location "/nettest">
    RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
    ProxyPreserveHost On
    ProxyPass  http://127.0.0.1:5000/
    ProxyPassReverse  http://127.0.0.1:5000/
</location>

应用程序找不到 css 文件。浏览器视图源码显示在html代码中呈现的是绝对路径:

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Home Page - WebApplication1</title>
    <link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="/css/site.css" />
</head>

如何强制呈现正确的 URL,例如。相对于当前文件夹,如

lib/bootstrap/dist/css/bootstrap.min.css

或绝对来自应用程序虚拟目录:

/nettest/lib/bootstrap/dist/css/bootstrap.min.css

** 更新**

更改顺序后找到 css 文件。但是示例应用程序模板中的隐私链接仍然无效。

ASP.NET 模板包含:

            <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                <ul class="navbar-nav flex-grow-1">
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                    </li>
                </ul>
            </div>

在浏览器中显示为

           <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                <ul class="navbar-nav flex-grow-1">
                    <li class="nav-item">
                        <a class="nav-link text-dark" href="/nettest">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-dark" href="/nettest/Home/Privacy">Privacy</a>
                    </li>
                </ul>
            </div>

主页链接有效,但隐私链接引发 404 错误。

在模板 HomeController 中定义:

public IActionResult Privacy()
        {
            return View();
        }

当应用程序在 Visual Studio 下运行时,它可以工作。当应用程序在 Apache 虚拟目录中运行时,如何使私有链接起作用?

配置方法:

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-2.2
        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });

        app.UseAuthentication();


        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        // origoli:            app.UseStaticFiles();

        //        https://stackoverflow.com/questions/46593999/how-to-host-a-asp-net-core-application-under-a-sub-folder

        // https://forums.asp.net/post/6327484.aspx
        //  because the middleware
        //app.UsePathBase("/nettest");
        //  strips the /nettest from the pipeline, the static file handler middleware needs to come first:
        app.UseStaticFiles(new StaticFileOptions
        {
            RequestPath = new PathString("/nettest")
        });
        app.UsePathBase("/nettest");
        // in asp.net core, you control the order of middleware and must get it correct.

        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }

【问题讨论】:

    标签: asp.net apache asp.net-core kestrel-http-server mod-proxy-html


    【解决方案1】:

    问题可能与您定义 UseStaticFiles 和 UseAuthentication 的顺序有关。将 UseStaticFiles 移到 UseAuthentication 上方。 更好的方法是使用 Apache 本身来渲染静态文件。

    【讨论】:

    • 在更改订单 css 字段后找到但隐私控制器调用仍然出现 404 错误。我更新了问题
    • 去掉 ProxyPass 和 ProxyPassReverse 之后的斜线。
    • ProxyPass http://127.0.0.1:5000/nettest(不带斜线)有效。谢谢。
    猜你喜欢
    • 2021-03-23
    • 1970-01-01
    • 2014-07-10
    • 2018-02-25
    • 1970-01-01
    • 2014-08-15
    • 2019-11-01
    • 1970-01-01
    • 2013-02-19
    相关资源
    最近更新 更多