c# - 由于 Release模式下的包缩小,jQuery 崩溃

标签 c# jquery asp.net-mvc bundle release-mode

我有 jquery 包:

bundles.Add(new ScriptBundle("~/bundle/jquery").Include(
                ScriptsPath("jquery-2.0.3.js"),
                ScriptsPath("jquery.validate.js"),
                ScriptsPath("jquery.validate.unobtrusive.js"),
                ScriptsPath("jquery-ui-1.10.3.js"),
                ScriptsPath("jquery.validate.unubtrusive.config.js"),
                ScriptsPath("jquery.easing.1.3.js "),
                ScriptsPath("jquery.unobtrusive-ajax.min.js"),
                ScriptsPath("jquery.validate.custom.attributes.js") ...

在用户注册页面上,我有登录和注册表单,因此表单输入的名称中有 Register.Login. 前缀。基本上它看起来像:

<input type="text" ... id="Register_Email" name="Register.Email" />
<input type="password" ... id="Register_Password" name="Register.Password" />

当我在 Release模式下发布我的应用程序时,我在 bundle 文件中收到此错误:

uncaught jquery error in bundle file

这显然是因为输入名称中的点。如何保存点并解决此问题?我已经尝试过 BundleTable.EnableOptimizations = false; 但它没有帮助,我不认为这是合适的解决方案,因为它破坏了 bundle 的目的。另请注意,问题仅发生在发布 模式下。

编辑: bundle 文件列表包含一个我自己的脚本文件,它包含我的 ForbidHtmlAttribude 的客户端验证逻辑:

jquery.validate.custom.attributes.js

jQuery.validator.unobtrusive.adapters.add(
    'forbidhtmlattribute',
    ['htmlregexpattern'],
    function (options) {
        options.rules['forbidhtmlattribute'] = options.params;
        options.messages['forbidhtmlattribute'] = options.message;
    }
);

jQuery.validator.addMethod('forbidhtmlattribute', function (value, element, params) {
    if (value === null || value === undefined) return true;

    var regex = params['htmlregexpattern'];
    return !value.match(regex);
}, '');

最佳答案

问题很可能出在这一行:

if (value === null || value === undefined) return true;

试着改成

if ((value === null) || (value === undefined)) return true;

解释:

MS 缩小算法删除了不必要的空格。它“知道”语言关键字,如“var”或“return”,但“null”不是其中之一。因此,缩小的行将是

if(value===null||value===undefined)return true;

现在从 JavaScript 的角度来看,我们有一个名为“null||value”的奇怪变量。将条件括在括号中解决了问题:

if(value===null)||(value===undefined)return true;

关于c# - 由于 Release模式下的包缩小,jQuery 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29735987/

相关文章:

c# - 获取电话号码设置

c# - 如何将选定的TreeView项目传递给RelayCommand,而无需后面的代码

c# - 调用设计模式的方法

c# - 从用户控件继承以使用不同的子控件

javascript - 同时更新两个动态文本字段

javascript - 复制 HTML div 的值

javascript - 如何在 jQuery 中以 block 的形式传递 html 表中的一些数据

c# - 具有图形的 Windows 8 C# 和 XAML 应用程序架构

asp.net-mvc - ASP.NET MVC Html.RadioButton 异常

asp.net-mvc - Razor MVC4 Url.Action 不起作用