Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为管理员用户角色呈现未绑定的资产_C#_Asp.net_Asp.net Mvc_Bundle_Bundling And Minification - Fatal编程技术网

C# 为管理员用户角色呈现未绑定的资产

C# 为管理员用户角色呈现未绑定的资产,c#,asp.net,asp.net-mvc,bundle,bundling-and-minification,C#,Asp.net,Asp.net Mvc,Bundle,Bundling And Minification,我是否可以为“管理员”角色的用户呈现未绑定和未统一的脚本和样式 我已经搜索并找到了如何禁用捆绑 BundleTable.EnableOptimizations = ... 和缩小 foreach (Bundle bundle in bundles) { bundle.Transforms.Clear(); } 在Global.asax.csApplication\u Start中,但我希望此逻辑是针对每个用户的,而不是针对每个应用程序实例的,因此它不应仅在应用程序启动时运行。首先,除

我是否可以为“管理员”角色的用户呈现未绑定和未统一的脚本和样式

我已经搜索并找到了如何禁用捆绑

BundleTable.EnableOptimizations = ...
和缩小

foreach (Bundle bundle in bundles)
{
    bundle.Transforms.Clear();
}

在Global.asax.cs
Application\u Start
中,但我希望此逻辑是针对每个用户的,而不是针对每个应用程序实例的,因此它不应仅在应用程序启动时运行。

首先,除非通过调用scripts.Render()或style.Render()将其包含在视图中,否则不会加载BundleConfig中包含的任何样式或脚本方法。默认情况下,从_Layout.schtml页面(视图/共享)调用这些方法

为管理区创建不同的布局页面并加载所需的脚本和样式将是一个更好的选择

也可以使用相同的方法Scripts.Render()从任何.cshtml文件(视图)加载这些文件。在这种情况下,它会像

@{
    if (User.IsInRole("Admin"))
    {
        //Update this portion with path of required bundles
        @Scripts.Render("~/bundles/jquery")
    }
    else
    {
        //Update this portion with path of required bundles
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryval")
    }
}
上述代码段可以添加到_布局页面或任何其他页面中

如果要求添加任何未绑定的代码。只需使用script标记。看起来是这样的:

@{
    if (User.IsInRole("Admin"))
    {
        //required script or style files
    }
    else
    {
        //To include unbundled and unminified scripts
        <script type="text/javascript" src="~/Scripts/jquery.validate.js"></script>
    }
}
@{
if(User.IsInRole(“Admin”))
{
//所需的脚本或样式文件
}
其他的
{
//包括未绑定和未统一的脚本
}
}

我对emodendroket建议的实施,目前对我来说效果很好:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new DisableBundlingForAdminFilter());
        // other filters
    }

    private class DisableBundlingForAdminFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #if !DEBUG
            BundleTable.EnableOptimizations = !filterContext.HttpContext.User.IsInRole("Admin");
            #endif
        }
    }
}

FilterConfig.RegisterGlobalFilters
Application\u Start

中被调用,这只是我的胡言乱语,但是动作过滤器呢?同意@emoderoket,您完全可以在操作筛选器中将BundleTable.EnableOptimizations设置为true/false。或者可能是
应用程序\u PostAuthenticateRequest
?我不想在我使用
@Scripts.Render
@style.Render
的任何地方都写这些类型的块。这不仅会涉及太多的文件,这意味着我必须“手动”拆分每个包,并为每个包中的所有内容维护单独的引用。就像我从jquery捆绑包中删除插件一样,我必须在某个管理块中查找它的引用,并将其删除。您可以在单个捆绑包中包含管理员用户的脚本和样式,并将此代码段添加到_Layout.cshtml页面中,这样您就不必在每个页面中编写它。我对你的问题有不同的理解。我以为你想为管理员用户包含不同的脚本和样式。我明白了。实际上,我希望在整个站点中为管理员和非管理员提供相同的资产,只是为管理员拆分。我不仅在我的_布局文件中有对JS&CSS的引用,而且在我的许多视图文件中也有对JS&CSS的引用,因此不适合将它们都包装在if块中。单包方法也不起作用,因为我不想要(例如:)一个jquery插件,用于在不同页面上运行的主页滑块,专用于一个有角度的webapp,反之亦然。
public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new DisableBundlingForAdminFilter());
        // other filters
    }

    private class DisableBundlingForAdminFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #if !DEBUG
            BundleTable.EnableOptimizations = !filterContext.HttpContext.User.IsInRole("Admin");
            #endif
        }
    }
}