Rendering shared assets for the administrator role

Is it possible for me to display disparate and uninstalled scripts and styles for users in the "Admin" role?

I searched and found how to disable binding

BundleTable.EnableOptimizations = ... 

and minimization

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

in Global.asax.cs Application_Start , but I want this logic to be for each user, and not for every instance of the application, so it should be launched not only when the application starts.

+6
source share
2 answers

My implementation of the emodendroket suggestion, which now works well for me:

 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 is called in Application_Start .

+3
source

First of all, none of the styles or scripts included in the BundleConfig will be loaded if it is not included in the view by calling the Scripts.Render () or Styles.Render () methods. By default, these methods are called on the _Layout.schtml (Views / Shared) page.

 @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") 

Creating another layout page for the admin area and loading the necessary scripts and styles would be the best option.

You can also load them from any .cshtml (view) file using the same method, Scripts.Render (). In this case, it will look like

 @{ 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") } } 

The above snippet can be added either to the _Layout page, or to any other page.

And if the requirement is to add some disparate code. Just use the script tag. What would look like this:

 @{ 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> } } 
+4
source

Source: https://habr.com/ru/post/973200/


All Articles