c# - 有没有办法防止通过 asp.net System.Web.Optimization 重命名某些变量

标签 c# asp.net bundling-and-minification

我在 AngularJS 项目中使用 ASP.Net 捆绑和缩小(System.Web.Optimization)。

除一种情况外,一切都运行良好。

此时,我们必须 eval() 一个表达式。我们知道这并不理想,但目前没有其他解决方案可以满足我们的需求。

所以我们有这一行:

var isValid = eval('globalJson.Permissions.' + attrs.drHasPermissionFor);

现在,因为缩小改变了我们变量的名称等 globalJson 不存在,所以这一行失败。

有没有办法指示 ASP.Net Minification 重命名特定变量?

更新

根据给出的答案 - 我们已经使用 Angulars 安全注入(inject)方式。

使用 this 的指令如下所示:

    appDirectives.directive('drHasPermissionFor', ['globalJson', function (globalJson) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                //code .....

                // how can I reference the injected globalJson here - so that i know it's using the
                // newly minified name..
                var isValid = eval('globalJson.Permissions.' + attrs.drHasPermissionFor);

//more code......
            }
        };
    }]);

最佳答案

好的 - 所以一位同事针对这个特定问题提出了解决方案。

它并没有真正回答“你能告诉优化让某些变量单独存在吗”这个整体问题,尽管它解决了手头的问题。

如果最初的问题得到回答,我很乐意更改已接受的答案。

现在,这可以像这样被破解:

appDirectives.directive('drHasPermissionFor', ['globalJson', function (globalJson) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {

            // this function will return the minified globalJson
            function returnGlobalJson() {
                return globalJson;
            };

            // call this function as part of the eval - works perfectly :)
            var isValid = eval('returnGlobalJson().Permissions.' + attrs.drHasPermissionFor);

        }
    };
}]);

关于c# - 有没有办法防止通过 asp.net System.Web.Optimization 重命名某些变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25793045/

相关文章:

c# - MVC3 Entity Framework 多对多附加列

c# - 使用 jQuery 将值传递给代码隐藏

ASP.NET/Web.config : customErrors redirect only on a 404

asp.net - Parent.FindControl() 不工作?

asp.net-mvc-4 - 仅在 mvc4 中启用捆绑而不是缩小

javascript - 将 JS 错误行号映射到未缩小的非捆绑 Assets 文件

c# mssql sql连接和多个插入

c# - 使用反射调用重写的基方法

c# - 我可以在要创建的对象的类对泛型类型参数有约束的泛型类中创建泛型对象吗?

javascript - Angular.js 的 ASP.NET 捆绑和缩小错误