我在我的ASP.NET Core和Angular 2应用程序中遇到了一个问题,它在开发过程中表现得非常好,但是当发布到IIS并为ASP.NET Core正确配置时,它无法加载所需的样式表和脚本。
我通过返回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控制器操作非常基本:
public IActionResult Index()
{
return new VirtualFileResult("~/index.html", "text/html");
}
在开发环境中运行良好,在生产环境中失败。我已经尝试将基本 href 更改为 ~/
,这将指向正确的应用程序根目录而不是服务器根目录的后续 url... 但它仍然无法在 /wwwroot/
文件夹中找到那些 css 文件或脚本。