Aspx到Razor语法转换器?

时间:2010-12-12 17:20:16

标签: c# asp.net-mvc asp.net-mvc-3 razor

我在C#中为MVC编写了大量的ASPX和ASCX文件,我想将它们转换为新的Razor语法。

任何人都知道一些可以加快这项工作的效用吗?

3 个答案:

答案 0 :(得分:29)

我写了一小段代码来进行转换。我认为这可能对其他人有用。我在途中学到了很多关于正则表达式balancing goup definitions的知识。

    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; 
    }
}

答案 1 :(得分:2)

试试这个

http://razorconverter.codeplex.com/

这是我编写的一个codeplex项目,它通过一些改进自动化Olma代码。还有一个需要.Net 2.0的exe下载,你可以将它放到你的视图文件夹中,它会自动转换所有内容。

答案 2 :(得分:1)

对于这样的工具来说有点太早了,考虑到Razor还没有测试版,只是在最新的SP中获得了语法高亮。

但是,google找到了this codeplex项目:

  

该项目仍处于早期阶段,因此没有任何二进制版本。您可以下载源代码。你需要VS2010来编译它。