0

我已经尝试了 squshit 和 rejuicer 来尝试从视图中组合和缩小一些 css 和 js。到目前为止还没有运气。我所做的是在我放置的过滤器中

#region using
    using System;
    using Castle.MonoRail.Framework;
    using campusMap.Models;
    using MonoRailHelper;
    using Rejuicer;
#endregion

namespace campusMap.Filters
{
    public class scriptFilter : IFilter
    {
        public bool Perform(ExecuteWhen exec, IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            controllerContext.PropertyBag["adminRejuicer_css"] = OnRequest.ForCss("~/Content/css/min/Combined.css").Compact;
            controllerContext.PropertyBag["adminRejuicer_js"] = OnRequest.ForJs("~/Content/js/min/Combined.js");
            //controllerContext.PropertyBag["JavaScriptBundle"] = new JavaScriptBundle();
            return true;
        }
    }
}

然后在基本控制器上

#region Directives
    using Castle.MonoRail.Framework;
    using campusMap.Services;
    using System.Text.RegularExpressions;
    using System;
    using campusMap.Models;
    using Castle.ActiveRecord;
    using MonoRailHelper;
    using campusMap.Filters;
    using log4net;
    using log4net.Config;
    using System.Text;

#endregion
namespace campusMap.Controllers
{
    [Filter(ExecuteWhen.BeforeAction, typeof(scriptFilter))]
    [Layout("default"), Rescue("generalerror")]
    public abstract class BaseController : MonoRailHelper.HelperBaseController
    {
    }

然后最后从视图中

    $adminRejuicer_css.File("~/Content/css/admin_styles.css").File("~/Content/js/colorpicker/css/jpicker-1.1.6.min.css")

但什么都没有。如果我对 ssushit 做基本相同的事情,但在过滤器中

    controllerContext.PropertyBag["cssBundle"] = Bundle.Css();

并且在视图中

    $cssBundle.Add("~/Content/css/admin_styles.css").Add("~/Content/js/colorpicker/css/jpicker-1.1.6.min.css").Render("~/Content/css/min/admin.css")

这次它至少只输出两个脚本的标签,没有缩小或捆绑,但至少它在做一些事情。

关于如何做到这一点的任何想法?谢谢你-杰里米

4

2 回答 2

0

您需要将其放在 Application_OnStart 方法中的 Global.asax.cs 文件中

OnRequest.ForCss("~/Content/css/min/Combined.css")

请参阅此博客文章(不是我的)在 aspnet 网站中添加 global.asax.cs 文件

于 2012-07-12T21:03:22.757 回答
0

所以这就是它的完成方式。这是使用squshit,但rejucier的操作方式相同。

基本控制器.cs

#region Directives 
    using Castle.MonoRail.Framework; 
    using campusMap.Services; 
    using campusMap.Models; 
    using Castle.ActiveRecord; 
    using MonoRailHelper; 
    using campusMap.Filters; 
    //any others you'd need
#endregion 
namespace campusMap.Controllers 
{ 
    [Filter(ExecuteWhen.BeforeAction, typeof(scriptFilter))] 
    [Layout("default"), Rescue("generalerror")] 
    public abstract class BaseController : MonoRailHelper.HelperBaseController 
    { 
        protected ScriptsService ScriptsService = new ScriptsService();
    } 
}

然后在过滤器中

脚本过滤器.cs

#region using
    using System;
    using Castle.MonoRail.Framework;
    using campusMap.Models;
    using MonoRailHelper;
    using campusMap.Services;
#endregion

namespace campusMap.Filters
{
    public class scriptFilter : IFilter
    {
        protected ScriptsService scriptsService = new ScriptsService();
        public bool Perform(ExecuteWhen exec, IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            controllerContext.PropertyBag["scriptsService"] = scriptsService;
            return true;
        }
    }
}

您会看到此时您正在为每个调用附加一个过滤器,所有内容都被渲染,并将对服务的引用放入属性包中,以便我们可以访问它。接下来是服务,因此我们可以提供访问方法。

ScriptsService.cs

#region Directives
    using System;
    using campusMap.Models;
    using MonoRailHelper;
    using campusMap.Services;
    using Castle.MonoRail.Framework;
    using campusMap.Filters;

    using SquishIt.Framework;
    using SquishIt.Framework.Css;
    using SquishIt.Framework.JavaScript;
    using System.Security.Cryptography;

#endregion

namespace campusMap.Services
{
    public class ScriptsService
    {
        public static string CalculateMD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }

        public static String Css(string files)
        {
            String name = CalculateMD5Hash(files);
            String path = @"/cache/script/css/";
            String FilePath = path + name + ".css";

            if (!HelperService.DirExists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            CSSBundle css = new CSSBundle();     
            foreach (string fl in files.Split(','))
            {
                css.Add(fl);
            }
            return css.ForceRelease().Render(FilePath);
        }
        public static String Js(string files)
        {

            String name = CalculateMD5Hash(files);
            String path = @"/cache/script/js/";
            String FilePath = path + name + ".js" ;

            if (!HelperService.DirExists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            JavaScriptBundle js = new JavaScriptBundle();
            foreach (string fl in files.Split(','))
            {
                js.Add(fl);
            }
            return js.ForceRelease().Render(FilePath);
        }
    }
}

因此,您需要在服务中注意的是,我们正在调用方法参数的 md5 哈希,它为我们提供了一个基本名称。

这就对了!!..查看易于设置。现在你如何使用它?

someView.vm

//CSS
$scriptsService.Css("~/Content/css/jquery-ui-1.8.19.custom.css,~/Content/css/central_main.css,~/Content/css/map_admin.css,~/Content/css/colorbox.css?")

//JS
$scriptsService.Js("~/Content/js/modernizr-2.0.6/modernizr.min.js,~/Content/js/jquery.defaultvalue.js,~/Content/js/utilities_general.js,~/Content/js/infobox.js,~/Content/js/jquery.ui.map.js")

需要注意的一件事是永远不要手动清除缓存。Squshit 会为您破坏客户端,因此不必担心触摸文件。

这里的所有都是它的。

于 2012-07-16T16:18:33.333 回答