Asp.net mvc ASP.NET核心中资源的相对url从开发到生产不起作用
我在ASP.NET Core&Angular 2应用程序中遇到了一个问题,它在开发中非常有效,但是当发布到IIS并为ASP.NET Core正确配置IIS时,它无法加载所需的样式表和脚本 通过返回Asp.net mvc ASP.NET核心中资源的相对url从开发到生产不起作用,asp.net-mvc,iis,asp.net-core,angular,relative-path,Asp.net Mvc,Iis,Asp.net Core,Angular,Relative Path,我在ASP.NET Core&Angular 2应用程序中遇到了一个问题,它在开发中非常有效,但是当发布到IIS并为ASP.NET Core正确配置IIS时,它无法加载所需的样式表和脚本 通过返回VirtualFileResult,我将所有未映射到API路由的请求重定向回index.html。html有一个 <!DOCTYPE html> <html> <head> <title>Data Platform</title>
VirtualFileResult
,我将所有未映射到API路由的请求重定向回index.html。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>
ClientPassthrough控制器操作非常基本:
public IActionResult Index()
{
return new VirtualFileResult("~/index.html", "text/html");
}
在开发中工作良好,在生产中失败惨重。我已尝试将基本href更改为
~/
,这将使后续URL指向正确的应用程序根而不是服务器根。。。但它似乎仍然无法在/wwwroot/
文件夹中找到这些css文件或脚本。我的方法是使用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>
在mvc配置中没有更多的客户端通过,我猜
如果完整路径在客户端端点调用中已经可用,我不知道为什么要返回它。如果您使用.NET Core with Razor,您可以添加以下代码,将您的基本URL设置为要发布到的IIS应用程序的URL
<base href="@(Url.Content("~"))" />
此代码应位于
元素正下方的
中
然后在引用根目录时使用
<link rel="stylesheet" href="lib/css/vendors.css" />
路径前面没有斜线。这就是我的工作
如果您尝试使用
或
,IIS将不知道如何处理它,因为除非它被.NET解析,否则它没有任何意义,而不是作为页面中的文本提供
希望这对其他人有所帮助。当你说找不到那些css文件时:如果index.html请求正确,你应该在chrome developer network中看到所有css文件请求,并在chrome中找到更多线索thereDo F12并检查network选项卡,这将告诉你它试图从何处加载文件,并为你提供有关问题的线索。我确实使用了chrome工具。它看起来像是在引用它应该引用的文件,domain/app/lib/css/styles.css或其他任何文件。tilde相对url也在用url做一些不可靠的事情。。。domain/app/~/lib/css/styles.css我真的搞不懂。我现在已经放弃了,我会更担心asp.NET的生产和RTM。当您部署到IIS时,您是使用子应用程序还是仅使用根站点?您真的需要客户端传递吗?无法为您配置:app.UseDefaultFiles();app.UseStaticFiles();
<base href="@(Url.Content("~"))" />
<link rel="stylesheet" href="lib/css/vendors.css" />