ASPX剃刀语法转换器? [英] Aspx to Razor syntax converter?

查看:95
本文介绍了ASPX剃刀语法转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当数量的ASPX和ASCX文件在C#中为所著MVC,我想将它们转换为新的Razor语法。

I have a considerable amount of ASPX and ASCX files writed in C# for MVC and I would like to convert them to the new Razor syntax.

任何身体知道一些实用工具,使这项工作得更快?

Any body knows about some utility that makes this job faster?

推荐答案

我已经写了一小块code,使转换。我认为这可能是别人有用。我已经学到了很多关于正则表达式的平衡党团定义的方式的。

I've written a small piece of code that makes the conversion. I think it could be useful to somebody else. I've learned a lot about regex's balancing goup definitions on the way.

    public static class RazorConversor
{
    public static void ConvertAll(string directory)
    {
        string[] array = Directory.GetFiles(directory, "*.aspx", SearchOption.AllDirectories).Concat(
                         Directory.GetFiles(directory, "*.ascx", SearchOption.AllDirectories)).ToArray();

        foreach (var fileName in array)
        {
            string aspxCode = File.ReadAllText(fileName);
            string razorCode = ConvertToRazor(aspxCode);
            File.WriteAllText(fileName, razorCode); //rename manually to update .csproj & source control
        }
    }

    static readonly string[] DefaultNamespaces = new string[]
    {
        "System.Web.Helpers", 
        "System.Web.Mvc",
        "System.Web.Mvc.Ajax",
        "System.Web.Mvc.Html",
        "System.Web.Routing",
        "System.Web.WebPages",
    };

    public static string ConvertToRazor(string aspxCode)
    {
        return ConvertToRazor(aspxCode, DefaultNamespaces); 
    }

    public static string ConvertToRazor(string aspxCode, string[] defaultNamespaces)
    {
        //namespaces
        string text2 = Regex.Replace(aspxCode, @"<%@\s+Import Namespace=""(?<ns>.*?)""\s+%>",
            m => defaultNamespaces.Contains(m.Groups["ns"].Value) ? null : "@using " + m.Groups["ns"].Value);

        //headers
        string text3 = Regex.Replace(text2, @"<%@\s(?<dir>.*?)%>", m =>  "@{ " + m.Groups["dir"].Value + "}"); // Preserves headers

        //expressions 
        string text4 = Regex.Replace(text3, @"<%[=:](?<expr>.*?)%>", m =>
        {
            string expr = m.Groups["expr"].Value.Trim();
            string cleanExpr = Regex.Replace(expr, @"(""(\\""|[^""])*"")|(@""([^""]|"""")*"")|(\([^\(\)]*(((?'Open'\()[^\(\)]*)+((?'Close-Open'\))[^\(\)]*)+)*\))", m2 => "");
            return cleanExpr.Contains(' ') ? "@(" + expr + ")" : "@" + expr;
        }, RegexOptions.Singleline);

        //code blocks
        string text5 = Regex.Replace(text4, @"<%(?<code>.*?)%>", m =>
        {
            string code = m.Groups["code"].Value.Trim();

            Dictionary<string, string> stringLiterals = new Dictionary<string,string>();

            code = Regex.Replace(code, @"(""(\\""|[^""])*"")|(@""([^""]|"""")*"")", m2 =>
            {
                string key = "<$" + stringLiterals.Count + "$>";
                stringLiterals.Add(key, m2.Value);
                return key;
            }); 

            string result = Regex.Replace(code, 
                @"((?<blockHeader>(else|(for|switch|foreach|using|while|if)\s*\([^\(\)]*(((?'Open'\()[^\(\)]*)+((?'Close-Open'\))[^\(\)]*)+)*\))\s*)" + 
                @"((?<fullBlock>{[^{}]*(((?'OpenCurly'{)[^{}]*)+((?'CloseCurly-OpenCurly'})[^{}]*)+)*})|(?<openblock>{.*))|" + 
                @"(?<text>((?!({|}|\s)(for|switch|foreach|using|while|if|else)(\s|{|\()).)+))",
                m2 =>
                {
                    if(m2.Value.Trim().Length == 0 || m2.Value.StartsWith("else")|| m2.Value.StartsWith("}"))
                        return m2.Value;

                    if(m2.Groups["text"].Success)
                        return "@{ " + m2.Value.Trim() + "}\r\n"; 

                    return "@" + m2.Value; 
                }, RegexOptions.ExplicitCapture | RegexOptions.Singleline);

            result = Regex.Replace(result, @"<\$\d+\$>", 
                m2 => stringLiterals[m2.Value]);

            return result;
        }, RegexOptions.Singleline);

        return text5; 
    }
}

这篇关于ASPX剃刀语法转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆