0

我刚刚执行了我的 MVC 应用程序的第一次部署,并且遇到了 Javascript 的一些问题。

为了在运行时挂钩,我这样做:

_Layout.cshtml

@if (IsSectionDefined("MyPage"))
{
    <script type="text/javascript">
        $(document).ready(function () {
            @RenderSection("MyPage", required: false)
        });
    </script>
}

我的页面.cshtml

然后在我的 Razor 视图中,我这样做:

@section MyPage
{
    myPage.SomeFunction();
}

myPage.js

(function (myPage, $) {

    myPage.SomeFunction = function () {
    ...
    };

}(window.myPage= window.myPage|| {}, jQuery));

这在 VS 中的 Debug 中效果很好,但是一旦我部署它(在 NuGet 包中使用 Otopus Deploy 进行连续部署),我就开始看到如下错误:

ReferenceError: myPage is not defined.

如果我查看源代码并点击捆绑链接,代码就在那里......

但是该文件的标题是缩小错误,例如:

/* Minification failed. Returning unminified contents.
(683,1-2): run-time warning JS1002: Syntax error: }
(683,51-52): run-time warning JS1002: Syntax error: }
 */
// Contains all the javascript required by the myPage page

我目前正在处理 Visual Studio 中的 JSLint 错误,但我认为这些更多是关于格式化而不是直接与 JS 上的 ReferenceError 相关,因为 JS 已加载,因为我可以从页面源链接到该函数。

关于看什么或如何解决这个问题的任何指示?

4

1 回答 1

0

This had me stumped for a moment.

All the Javascript was there (linkable from bundle links in the page source), all the files were there, everything worked in dev (doesn’t it always?).

Anyway, if you’re seeing this error in your deployed applications there are a couple of steps to take.

Firstly you should download and install the Visual Studio tool JSLint.VS2012. Then set this up to show warnings and don’t set it up to fail the build. JSLint is not kind and very strict about things (your adherence to BP is your choice but recommended for sure!)

So, you deploy your app and BANG all your lovely JS is broken. Never fear … pop over to your web app Global.asax file and in the Application_Start method include this:

BundleTable.EnableOptimizations = true;

So now with this setting set you can launch your app in debug mode within Visual Studio with all the optimizations forced on to test minification and more closely model your testing to the deployed version.

Once you have that working and are seeing the problems more clearly you can start to work through the potential issues using the reports from your new JSLint VS plugin to fix the syntax and other formatting issues.

于 2013-08-31T13:16:50.653 回答