Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <a href="http://msdn.microsoft.com/en-us/library/bs2twtah.aspx#balancing_group_definition">balancing goup definitions</a> on the way. </p> <pre><code> 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 &amp; 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, @"&lt;%@\s+Import Namespace=""(?&lt;ns&gt;.*?)""\s+%&gt;", m =&gt; defaultNamespaces.Contains(m.Groups["ns"].Value) ? null : "@using " + m.Groups["ns"].Value); //headers string text3 = Regex.Replace(text2, @"&lt;%@\s(?&lt;dir&gt;.*?)%&gt;", m =&gt; "@{ " + m.Groups["dir"].Value + "}"); // Preserves headers //expressions string text4 = Regex.Replace(text3, @"&lt;%[=:](?&lt;expr&gt;.*?)%&gt;", m =&gt; { string expr = m.Groups["expr"].Value.Trim(); string cleanExpr = Regex.Replace(expr, @"(""(\\""|[^""])*"")|(@""([^""]|"""")*"")|(\([^\(\)]*(((?'Open'\()[^\(\)]*)+((?'Close-Open'\))[^\(\)]*)+)*\))", m2 =&gt; ""); return cleanExpr.Contains(' ') ? "@(" + expr + ")" : "@" + expr; }, RegexOptions.Singleline); //code blocks string text5 = Regex.Replace(text4, @"&lt;%(?&lt;code&gt;.*?)%&gt;", m =&gt; { string code = m.Groups["code"].Value.Trim(); Dictionary&lt;string, string&gt; stringLiterals = new Dictionary&lt;string,string&gt;(); code = Regex.Replace(code, @"(""(\\""|[^""])*"")|(@""([^""]|"""")*"")", m2 =&gt; { string key = "&lt;$" + stringLiterals.Count + "$&gt;"; stringLiterals.Add(key, m2.Value); return key; }); string result = Regex.Replace(code, @"((?&lt;blockHeader&gt;(else|(for|switch|foreach|using|while|if)\s*\([^\(\)]*(((?'Open'\()[^\(\)]*)+((?'Close-Open'\))[^\(\)]*)+)*\))\s*)" + @"((?&lt;fullBlock&gt;{[^{}]*(((?'OpenCurly'{)[^{}]*)+((?'CloseCurly-OpenCurly'})[^{}]*)+)*})|(?&lt;openblock&gt;{.*))|" + @"(?&lt;text&gt;((?!({|}|\s)(for|switch|foreach|using|while|if|else)(\s|{|\()).)+))", m2 =&gt; { 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, @"&lt;\$\d+\$&gt;", m2 =&gt; stringLiterals[m2.Value]); return result; }, RegexOptions.Singleline); return text5; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload