VS 2012中的HTTP处理程序和javascript绑定

VS 2012中的HTTP处理程序和javascript绑定,javascript,asp.net,localization,httphandler,bundling-and-minification,Javascript,Asp.net,Localization,Httphandler,Bundling And Minification,我目前正在尝试设置一个项目来实现javascript文件的本地化(如上所述),但同时我希望捆绑并缩小项目中的javascript。我遵循了关于捆绑和缩小的教程 我已经能够让两者分开工作,但当我试图让它们一起工作时,我无法让本地化正常工作。我认为这是因为绑定为它生成的绑定/缩小的javascript创建了自己的路由处理,因此我在webconfig中定义的httpHandler被忽略。我不断收到javascript错误,说“CustomTranslate未定义” 我之所以这么做是因为我们正在使用Ex

我目前正在尝试设置一个项目来实现javascript文件的本地化(如上所述),但同时我希望捆绑并缩小项目中的javascript。我遵循了关于捆绑和缩小的教程

我已经能够让两者分开工作,但当我试图让它们一起工作时,我无法让本地化正常工作。我认为这是因为绑定为它生成的绑定/缩小的javascript创建了自己的路由处理,因此我在webconfig中定义的httpHandler被忽略。我不断收到javascript错误,说“CustomTranslate未定义”

我之所以这么做是因为我们正在使用ExtJS构建许多控件,但我们需要能够对这些控件应用本地化。如果我能帮助他们一起工作,我将不胜感激

我不是在使用MVC,而是在Visual Studio 2012的asp.net中使用MVC

这是我的密码:

捆绑图

namespace TranslationTest
{
    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            //default bundles addeed here...

            bundles.Add(new ScriptBundle("~/bundles/ExtJS.axd").Include("~/Scripts/ExtJS/ext-all.js", "~/Scripts/ExtJS/TestForm.js"));

        }
    }
}    
web.config:

<globalization uiCulture="auto" />
<httpHandlers>
  <add verb="*" path="/bundles/ExtJS.axd" type="TranslationTest.ScriptTranslator, TranslationTest" />
</httpHandlers>
目前,FirstName、LastName等都存储在资源文件中,如上面链接的示例所示

ScriptTranslator.cs

namespace TranslationTest
{
    public class ScriptTranslator : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string relativePath = context.Request.AppRelativeCurrentExecutionFilePath.Replace(".axd", string.Empty);
            string absolutePath = context.Server.MapPath(relativePath);
            string script = ReadFile(absolutePath);
            string translated = TranslateScript(script);

            context.Response.Write(translated);

            Compress(context);
            SetHeadersAndCache(absolutePath, context);
        }

        #endregion

        private void SetHeadersAndCache(string file, HttpContext context)
        {
            context.Response.AddFileDependency(file);
            context.Response.Cache.VaryByHeaders["Accept-Language"] = true;
            context.Response.Cache.VaryByHeaders["Accept-Encoding"] = true;
            context.Response.Cache.SetLastModifiedFromFileDependencies();
            context.Response.Cache.SetExpires(DateTime.Now.AddDays(7));
            context.Response.Cache.SetValidUntilExpires(true);
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
        }

        #region Localization

        private static Regex REGEX = new Regex(@"CustomTranslate\(([^\))]*)\)", RegexOptions.Singleline | RegexOptions.Compiled);

        private string TranslateScript(string text)
        {
            MatchCollection matches = REGEX.Matches(text);
            ResourceManager manager = new ResourceManager(typeof(TranslationTest.App_GlobalResources.text));

            foreach (Match match in matches)
            {
                object obj = manager.GetObject(match.Groups[1].Value);
                if (obj != null)
                {
                    text = text.Replace(match.Value, CleanText(obj.ToString()));
                }
            }
            return text;
        }

        private static string CleanText(string text)
        {
            text = text.Replace("'", "\\'");
            text = text.Replace("\\", "\\\\");
            return text;
        }

        private static string ReadFile(string absolutePath)
        {
            if (File.Exists(absolutePath))
            {
                using (StreamReader reader = new StreamReader(absolutePath))
                {
                    return reader.ReadToEnd();
                }
            }
            return null;
        }

        #endregion

        #region Compression

        private const string GZIP = "gzip";
        private const string DEFLATE = "deflate";

        private static void Compress(HttpContext context)
        {
            if (IsEncodingAccepted(DEFLATE, context))
            {
                context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
                SetEncoding(DEFLATE, context);
            }
            else if (IsEncodingAccepted(GZIP, context))
            {
                context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
                SetEncoding(GZIP, context);
            }
        }

        private static bool IsEncodingAccepted(string encoding, HttpContext context)
        {
            return context.Request.Headers["Accept-encoding"] != null && context.Request.Headers["Accept-encoding"].Contains(encoding);
        }

        private static void SetEncoding(string encoding, HttpContext context)
        {
            context.Response.AppendHeader("Content-encoding", encoding);
        }

        #endregion

    }
}
global.asax.cs

namespace TranslationTest
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            Microsoft.Web.Optimization.BundleTable.Bundles.EnableDefaultBundles();

            BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();
        }
    }
}
void Application_Start(object sender, EventArgs e)
        {
            //Microsoft.Web.Optimization.BundleTable.Bundles.EnableDefaultBundles(); 
            BundleTable.EnableOptimizations = true; //Added this line..
            BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();
        }

我希望我已经涵盖了一切,但如果有什么遗漏,请告诉我。提前谢谢

好的,我已经在您的示例中设置好了所有内容,并且可以正常工作,但是您需要使用IBundleTransform接口。我所做的每件事的细节都贴在下面

我必须创建一个类来处理bundle转换(即转换),而不是允许默认行为

public class JsLocalizationTransform : IBundleTransform
    {
        public JsLocalizationTransform(){}

        #region IBundleTransform Members

        public void Process(BundleContext context, BundleResponse response)
        {
            string translated = TranslateScript(response.Content);

            response.Content = translated;
        }

        #endregion

        #region Localization

        private static Regex REGEX = new Regex(@"CustomTranslate\(([^\))]*)\)", RegexOptions.Singleline | RegexOptions.Compiled);

        private string TranslateScript(string text)
        {
            MatchCollection matches = REGEX.Matches(text);
            ResourceManager manager = new ResourceManager(typeof(TranslationTest.App_GlobalResources.text));

            foreach (Match match in matches)
            {
                object obj = manager.GetObject(match.Groups[1].Value);
                if (obj != null)
                {
                    text = text.Replace(match.Value, CleanText(obj.ToString()));
                }
            }

            return text;
        }

        private static string CleanText(string text)
        {
            //text = text.Replace("'", "\\'");
            text = text.Replace("\\", "\\\\");

            return text;
        }
        #endregion

    }
然后在BundleConfig.RegisterBundles方法中,您需要创建并添加如下捆绑包:

var extjsBundle = new Bundle("~/bundles/ExtJS").Include("~/Scripts/ExtJS/ext-all.js", "~/Scripts/ExtJS/TestForm.js");
    extjsBundle.Transforms.Clear();
    extjsBundle.Transforms.Add(new JsLocalizationTransform());
    extjsBundle.Transforms.Add(new JsMinify());
    bundles.Add(extjsBundle);
然后,我可以从web.config中删除
HttpHandler
,因为它是通过bundler自动配置的。我还必须对global.asax.cs中的应用程序启动方法进行一些更改

namespace TranslationTest
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            Microsoft.Web.Optimization.BundleTable.Bundles.EnableDefaultBundles();

            BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();
        }
    }
}
void Application_Start(object sender, EventArgs e)
        {
            //Microsoft.Web.Optimization.BundleTable.Bundles.EnableDefaultBundles(); 
            BundleTable.EnableOptimizations = true; //Added this line..
            BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();
        }
因为
JSLocalisationTransform
类正在处理bundle转换和翻译,所以我完全删除了
ScriptTranslator


希望这能有所帮助。

顺便说一句,我也尝试过让我的脚本转换器实现IRouteHandler,但在这方面也没有太多运气。您如何处理缓存和语言更改?