Convert single aspx file to code behind

I am working on a website (and not a web application) in VS 2008.Net 3.5, and it uses a single .aspx model where server code is included in the html head instead of using it. The aspx.cs code behind the page.

I would like to quickly convert the files to use the model with the codename, but for now the only way to do this is to delete the file by creating a new aspx page with the same name, then manually copy the code into the .aspx page and the server code into the .aspx page .cs.

Is there a faster way to do this?

I saw two articles that seem to answer this question, but unfortunately not: Working with single form web form pages in Visual Studio.NET and How to convert an aspx file or main page to page and code?

Both offer a simple solution, according to which VS works on the foot, you just point it to the file and shoot. For some reason they do not work. The first article seems to relate to VS 2002, and the second seems to relate to a web application.

Is there any hope for a website?

Also, maybe I see it wrong, is there an advantage for a single-page model? I plan to convert the entire website to a web application soon, is one page working well in web applications?

+6
visual-studio-2008 code-behind
source share
5 answers

If manual conversion is too time consuming and automatic conversion does not work, I think that your only option is to create your own converter. You can write a simple console application that takes a directory path on the command line and processes each file in that directory. This is not too complicated - here I will start:

using System; using System.IO; class Program { const string ScriptStartTag = "<script language=\"CS\" runat=\"server\">"; const string ScriptEndTag = "</script>"; static void Main(string[] args) { DirectoryInfo inPath = new DirectoryInfo(args[0]); DirectoryInfo outPath = new DirectoryInfo(args[0] + "\\codebehind"); if (!outPath.Exists) inPath.CreateSubdirectory("codebehind"); foreach (FileInfo f in inPath.GetFiles()) { if (f.FullName.EndsWith(".aspx")) { // READ SOURCE FILE string fileContents; using (TextReader tr = new StreamReader(f.FullName)) { fileContents = tr.ReadToEnd(); } int scriptStart = fileContents.IndexOf(ScriptStartTag); int scriptEnd = fileContents.IndexOf(ScriptEndTag, scriptStart); string className = f.FullName.Remove(f.FullName.Length-5).Replace("\\", "_").Replace(":", "_"); // GENERATE NEW SCRIPT FILE string scriptContents = fileContents.Substring( scriptStart + ScriptStartTag.Length, scriptEnd-(scriptStart + ScriptStartTag.Length)-1); scriptContents = "using System;\n\n" + "public partial class " + className + " : System.Web.UI.Page\n" + "{\n" + " " + scriptContents.Trim() + "\n}"; using (TextWriter tw = new StreamWriter(outPath.FullName + "\\" + f.Name + ".cs")) { tw.Write(scriptContents); tw.Flush(); } // GENERATE NEW MARKUP FILE fileContents = fileContents.Remove( scriptStart, scriptEnd - scriptStart + ScriptEndTag.Length); int pageTagEnd = fileContents.IndexOf("%>"); fileContents = fileContents.Insert(PageTagEnd, "AutoEventWireup=\"true\" CodeBehind=\"" + f.Name + ".cs\" Inherits=\"" + className + "\" "); using (TextWriter tw = new StreamWriter(outPath.FullName + "\\" + f.Name)) { tw.Write(fileContents); tw.Flush(); } } } } } 

30 minutes of coding, 30 minutes of debugging. There are some obvious errors - for example, if your code contains a closing script tag somewhere inside , then it will not be correctly exported. The results will not be good, but this should take care of 90% of your code, and you should be able to manually clean up any problems. It helps?

+19
source share

Basically you need to create a class file. Inherit the class from System.Web.UI.Page, and then change the page directive on the page to point to the code behind.

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %> 

Where Inherits is the name of your class file, and CodeBehind is the code file you just created. You may need to reload the project to make explorer Explorer display the file as an attachment, but even if you do not, it should work.

You can also check the accepted answer for the alternative. How does IIS know if it serves a website or web application project?

+3
source share

I don’t know what to do, to be honest.

It is probably best to create a new page and copy it to paste until everything works, and then delete the source code, rename the new file to the old name and rebuild.

Not ideal, but probably the fastest / cleanest / safest way to migrate.

+2
source share

Thank you so much! Here is a small modified version if your code is written by VB.Net. Just compile and run the converter in each folder containing aspx sites.

 using System.IO; namespace Converter { class Program { const string ScriptStartTag = "<script runat=\"server\">"; const string ScriptEndTag = "</script>"; static void Main(string[] args) { string currentDirectory = System.Environment.CurrentDirectory; var inPath = new DirectoryInfo(currentDirectory); var outPath = new DirectoryInfo(currentDirectory); if (!outPath.Exists) inPath.CreateSubdirectory("codebehind"); foreach (FileInfo f in inPath.GetFiles()) { if (f.FullName.EndsWith(".aspx")) { // READ SOURCE FILE string fileContents; using (TextReader tr = new StreamReader(f.FullName)) { fileContents = tr.ReadToEnd(); } int scriptStart = fileContents.IndexOf(ScriptStartTag); int scriptEnd = fileContents.IndexOf(ScriptEndTag, scriptStart); string className = f.FullName.Remove(f.FullName.Length - 5).Replace("\\", "_").Replace(":", "_"); // GENERATE NEW SCRIPT FILE string scriptContents = fileContents.Substring( scriptStart + ScriptStartTag.Length, scriptEnd - (scriptStart + ScriptStartTag.Length) - 1); scriptContents = "Imports System\n\n" + "Partial Public Class " + className + " \n Inherits System.Web.UI.Page\n" + "\n" + " " + scriptContents.Trim() + "\nEnd Class\n"; using (TextWriter tw = new StreamWriter(outPath.FullName + "\\" + f.Name + ".vb")) { tw.Write(scriptContents); tw.Flush(); } // GENERATE NEW MARKUP FILE fileContents = fileContents.Remove( scriptStart, scriptEnd - scriptStart + ScriptEndTag.Length); int pageTagEnd = fileContents.IndexOf("%>"); fileContents = fileContents.Insert(pageTagEnd, "AutoEventWireup=\"false\" CodeBehind=\"" + f.Name + ".vb\" Inherits=\"" + className + "\" "); using (TextWriter tw = new StreamWriter(outPath.FullName + "\\" + f.Name)) { tw.Write(fileContents); tw.Flush(); } } } } } } 
+1
source share

if your aspx file has 2 sections, and you can split it mechanically, why don’t you write only a small parser to automate the work? It shouldn't be difficult, it's just text manipulation and finding a recursive file.

0
source share

All Articles