ASP.NET Core 中资源的相对 url 无法从开发到生产

Cow*_*man 5 iis asp.net-mvc relative-path asp.net-core angular

我正在努力解决我的 ASP.NET Core 和 Angular 2 应用程序中的一个问题,它在开发中运行良好,但是当发布到 IIS 并为 ASP.NET Core 正确配置 IIS 时,它无法加载所需的样式表和脚本。

我将所有没有映射到我的 API 路由的请求通过返回一个VirtualFileResult. index.html 有一个

<!DOCTYPE html>
<html>
<head>
    <title>Data Platform</title>
    <meta name="description" content="Data Platform" />
    <meta charset="utf8" />

    <base href="/" />
    <link rel="stylesheet" href="lib/css/vendors.css" />
    <link rel="stylesheet" href="platform/content/css/base.css" />
    <link rel="stylesheet" href="platform/content/css/bootstrap-overrides.css" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />

    <script src="lib/vendors.js"></script>
    <script>
        System.config({
            packages: {
                "/platform/": { defaultExtension: "js" }
            }
        });

        System.import("/platform/boot");
    </script>
</head>
<body>
    <data-platform>Initializing...</data-platform>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Startup.cs配置是非常基本的:

app.UseIISPlatformHandler();

if (string.Equals(env.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
{
    app.UseDeveloperExceptionPage();
}

app.UseFileServer(false);
app.UseStatusCodePages();
app.UseMvc(routeBuilder =>
{
    routeBuilder.MapRoute(
        name: "Api",
        template: "api/{controller}/"
    );
    routeBuilder.MapRoute(
        name: "Client Passthrough",
        template: "{*any}",
        defaults: new { Controller = "ClientPassthrough", Action = "Index" }
    );
});
Run Code Online (Sandbox Code Playgroud)

ClientPassthrough 控制器操作非常基础:

public IActionResult Index()
{
    return new VirtualFileResult("~/index.html", "text/html");
}
Run Code Online (Sandbox Code Playgroud)

在开发中运行良好,在生产中惨遭失败。我已经尝试将基本 href 更改为~/将后续 url 指向正确的应用程序根目录而不是服务器根目录......但它似乎仍然无法在/wwwroot/文件夹中找到那些 css 文件或脚本。

vin*_*nzo 0

我的方法是使用 url 重写,也许在这篇文章中你也会得到更多提示,以防你想尝试:

<rewrite>
  <rules>
    <!--Redirect selected traffic to index -->
    <rule name="Index Rule" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
      </conditions>
      <action type="Rewrite" url="/index.html" />
    </rule>
  </rules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)

一起,在Startup.cs文件中:

 app.UseDefaultFiles();

 app.UseStaticFiles();
Run Code Online (Sandbox Code Playgroud)

我想,mvc 配置中不再有客户端了...

我不明白为什么应该返回完整路径(如果它在客户端端点调用中已经可用)。