在与.cs文件和 RequireJS 相关的生产服务器中部署问题



我有一个正在使用 ASP.Net 开发的项目。并使用 Elastic Beanstalk 在 AWS 上安装生产服务器。 问题是requireJS之后的JS文件不会使用我制作的帮助程序加载。但是当我调试时,在我的本地PC上,它工作得很好!

我添加了requireJS,对其进行了配置,并添加了一个帮助程序,以根据呈现的视图定位和使用特定的JS文件。使用 Web 部署包发布项目后,我在相应的应用程序中使用 Elastic Beanstalk 进行上传和部署。它进展顺利,但是当我访问生产现场时,JS将无法正常工作。帮助程序没有加载 JS 文件,但在我的调试本地 PC 中它加载了。

这是我在帮助程序中调用方法的页脚:

@using ypf.Helper
<footer>
...
</footer>
<script src="~/Scripts/dist/require.js"></script>
<script>
@Html.ViewSpecificRequireJS()
</script>
</body>
</html>

这是帮助程序: RequireJSHelper.cs

using System;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace ypf.Helper
{
public static class RequireJsHelpers
{
public static MvcHtmlString RequireJs(this HtmlHelper helper, string config, string module)
{
var require = new StringBuilder();
string jsLocation = "/Scripts/";

if (File.Exists(helper.ViewContext.HttpContext.Server.MapPath(Path.Combine(jsLocation, module + ".js"))))
{
require.AppendLine("require( [ "" + jsLocation + config + "" ], function() {");
require.AppendLine("    require( [ "" + module + ""] );");
require.AppendLine("});");
}
return new MvcHtmlString(require.ToString());
}
public static MvcHtmlString ViewSpecificRequireJS(this HtmlHelper helper)
{
var action = helper.ViewContext.RouteData.Values["action"];
var controller = helper.ViewContext.RouteData.Values["controller"];
var namespaces = helper.ViewContext.RouteData.DataTokens["namespaces"];
if (namespaces != null)
{
string namespacesToString = ((string[])namespaces)[0].Replace(".", " ");
string[] namespacesWordList = namespacesToString.Split(' ');
string namespacesWord1 = ((string[])namespacesWordList)[2];
string namespacesWord2 = ((string[])namespacesWordList)[3];
return helper.RequireJs("config.js", string.Format("views/{0}/{1}/{2}/{3}", namespacesWord1, namespacesWord2, controller, action));
} else
{
return helper.RequireJs("config.js", string.Format("views/{0}/{1}", controller, action));
}
}
}
}

所以我想要的是渲染后生产上的输出:


</footer>
<script src="/Scripts/dist/require.js"></script>
<script>
require( [ "/Scripts/config.js" ], function() {
require( [ "views/Home/Index"] );
});
</script>

相反,我得到这个:

</footer>
<script src="/Scripts/dist/require.js"></script>
<script>
</script>

同样,它在本地调试时提供了预期的输出,但在我使用 Elastic Beanstalk 上传和部署.zip后,它就不会在生产服务器中提供

溶液:

让我为那些阅读本文的人澄清一下,这个问题不是由 AWS Beanstalk 引起的。它可能发生在任何云服务中,因为问题出在我创建的文件中。

感谢@BowB@Abdul的回答。 该语句返回 false,因为变量jsLocation = "/Scripts/"没有~,并且在生产中呈现时,它需要服务器~符号才能了解在哪里可以找到 Scripts 文件夹。

所以我的第一次尝试是我~添加到jsLocation的值中,就像jsLocation = "~/Scripts/"一样,然后再次上传和部署,它就像一个魅力,但是当我在本地调试时,它不知道在 RequireJSrequire声明中在哪里可以找到 Scripts 文件夹:require(["~/Scripts/config.js"])

似乎在构建部署包时,C# 编译和 RequireJS 以不同的方式理解~符号。如果在 C# 文件中找到~符号,它理解的是文件夹脚本所在的根,但在 RequireJS 中,它理解其他地方或不会允许~

所以最终的解决方案是我在 IF 语句中的变量jsLocation之前连接了"~"if (File.Exists(helper.ViewContext.HttpContext.Server.MapPath(Path.Combine("~" + jsLocation, module + ".js"))))

而不是将其添加到jsLocation的值

现在,它在"调试"和"生产"中找到"脚本"文件夹。 对于尝试在可能需要此解决方案的 .Net 项目中使用 requireJS 的任何人。

这个 if 语句在您的部署中返回 false:

if (File.Exists(helper.ViewContext.HttpContext.Server.MapPath(Path.Combine(jsLocation, module + ".js"))))

检查/Scripts/views/Home/Index.js 文件是否正在复制到生产部署配置中的正确位置。这可能取决于您使用的 IDE。

是的,似乎是文件不存在的问题,请检查文件是否存在于以下代码中提到的位置。

if (File.Exists(helper.ViewContext.HttpContext.Server.MapPath(Path.Combine(jsLocation, module + ".js"))))

最新更新