Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aspx to Razor syntax converter? [closed]

Tags:

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?

like image 677
Olmo Avatar asked Dec 12 '10 17:12

Olmo


People also ask

Why use a Razor when we have aspx?

Razor has new and advance syntax that are compact, expressive and reduces typing. Web Form Engine has the same syntax like Asp.net Web Forms uses for . aspx pages. By default, Razor Engine prevents XSS attacks(Cross-Site Scripting Attacks) means it encodes the script or html tags like <,> before rendering to view.

Which is faster ASPX or Razor?

Aspx Engine is faster compared to Razor Engine.

What is the Razor syntax?

Razor syntax is a simple programming syntax for embedding server-based code in a web page. In a web page that uses the Razor syntax, there are two kinds of content: client content and server code.

Which view engine is better Razor or ASPX?

The Razor View Engine is a bit slower than the ASPX View Engine. Razor provides a new view engine with streamlined code for focused templating. Razor's syntax is very compact and improves readability of the markup and code. By default MVC supports ASPX (web forms) and Razor View Engine.


2 Answers

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;      } } 
like image 193
Olmo Avatar answered Sep 28 '22 06:09

Olmo


Try this

http://razorconverter.codeplex.com/

It is a codeplex project that I wrote that automates Olma code with some refinements. There is also an exe download that requires .Net 2.0 I think and you can just drop it into your view folder and it converts everything automatically.

like image 40
Al Katawazi Avatar answered Sep 28 '22 04:09

Al Katawazi