下面是最終解決了這個問題。基本上,我們使用由「ember build」創建的index.html,並在運行時找到了從environment.js傳遞給javascript的元標記。通過在index.cshtml中生成元標記,我們可以使用web.config中指定的值。
現在,當我們構建應用程序切換時,我們簡單地忽略它生成的index.html,但將其餘資源添加到預製的Visual Studio Web項目中。該項目可能配置爲測試或生產。
首先,我在Visual Studio中創建了一個空的Web應用程序。在web.config中,我添加了這個:
<appSettings>
<add key="hostName" value="https://apps.example.com/" />
<add key="baseURL" value="/projectDir/" />
<add key="serverNamespace" value="api" />
<add key="imgSrc" value="https://images.example.com" />
</appSettings>
,然後index.cshtml被寫入利用這些值:
@{
if (!Request.Url.AbsoluteUri.EndsWith("/"))
{
Response.Redirect(Request.Url.AbsoluteUri + "/");
}
var configStr = @"{{
""modulePrefix"": ""foo-bar"",
""podModulePrefix"": ""foo-bar/pods"",
""environment"": ""production"",
""baseURL"": ""{3}"",
""locationType"": ""hash"",
""namespace"": ""{0}"",
""host"": ""{1}"",
""foo-auth-host"": ""{1}"",
""APP"": {{
""name"": ""foo-bar"",
""version"": ""0.0.0""
}},
""contentSecurityPolicy"": {{
""default-src"": ""'none'"",
""script-src"": ""'self'"",
""font-src"": ""'self'"",
""img-src"": ""'self' {2}"",
""style-src"": ""'self'"",
""media-src"": ""'self'"",
""connect-src"": ""'self' {1}""
}},
""simple-auth"": {{
""authorizer"": ""authorizer:foo"",
""routeAfterAuthentication"": ""/"",
""store"": ""simple-auth-session-store:local-storage""
}},
""contentSecurityPolicyHeader"": ""Content-Security-Policy-Report-Only"",
""exportApplicationGlobal"": false
}}";
var serverNamespace = System.Web.Configuration.WebConfigurationManager.AppSettings["serverNamespace"];
var hostName = System.Web.Configuration.WebConfigurationManager.AppSettings["hostName"];
var imgSrc = System.Web.Configuration.WebConfigurationManager.AppSettings["imgSrc"];
var baseUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["baseURL"];
var configStrPopulated = string.Format(configStr, serverNamespace, hostName, imgSrc, baseUrl);
var configStrCompacted = System.Text.RegularExpressions.Regex.Replace(configStrPopulated, @"\s+", "");
var configStrEncoded = HttpUtility.UrlEncode(configStrCompacted);
}<!DOCTYPE html>
<html>
<head>
...
<meta name="foo-bar/config/environment" content="@configStrEncoded" />
...
</head>
<body>
<script src="assets/vendor.js"></script>
<script src="assets/foo-bar.js"></script>
</body>
</html>
感謝包!當我搜索基地標籤(我從來沒有見過),我發現這個:https://github.com/ember-cli/ember-cli/issues/2633和http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag,這讓我非常緊張。但這與我想到的解決方案非常接近。我將不得不與正在構建項目餘燼部分的隊友交談。 – phil
@phil哇這樣的戲劇化在那個線程。我非常清楚' '標籤是如何工作的,它的字面意思就是這種情況。弄清楚你的特定情況下是否有阻止你使用它的事情,這只是一個工具。 :) –
我不認爲我們會,我們不使用SVG或任何不尋常的東西。所以我認爲在這種情況下,它會按照你的建議工作。再次感謝! – phil