gpt4 book ai didi

asp.net-mvc - ASP.NET Core 中资源的相对 URL 从开发到生产都不起作用

转载 作者:太空狗 更新时间:2023-10-29 19:35:48 25 4
gpt4 key购买 nike

我在我的 ASP.NET Core 和 Angular 2 应用程序中遇到了一个问题,它在开发中工作得很好,但是当发布到 IIS 并为 ASP.NET Core 正确配置 IIS 时,它无法加载所需的样式表和脚本。

我通过返回 VirtualFileResult 将所有未映射到我的 API 路由的请求重定向回 index.html。 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>

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" }
);
});

ClientPassthrough Controller 操作非常基本:

public IActionResult Index()
{
return new VirtualFileResult("~/index.html", "text/html");
}

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

最佳答案

如果您将 .NET Core 与 Razor 结合使用,您可以添加以下代码以将您的基本 URL 设置为您要发布到的 IIS 应用的 URL。

<base href="@(Url.Content("~"))" />

此代码应放在 <head> 中就在 <title> 下面元素。

然后当你引用root时使用

<link rel="stylesheet" href="lib/css/vendors.css" />

路径前没有斜杠。这对我有用。

如果您尝试使用 <base href="/" /><base href="~/" /> , IIS 不知道如何处理它,因为代字号没有任何意义,除非它被 .NET 解析,而不是作为页面中的文本提供。

希望这对某人有帮助。

关于asp.net-mvc - ASP.NET Core 中资源的相对 URL 从开发到生产都不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35076761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com