Convert a single file aspx to code behind

扶醉桌前 提交于 2019-12-04 04:09:56

If manual conversion is too time-intensive, and the automatic conversion isn't working, I think your only other option would be to build your own converter. You could write a simple console app which takes a directory path on the command line and processes every file in that directory. This isn't too hard - here, I'll get you started:

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 coding, 30 minutes debugging. There are some obvious bugs - like, if your code contains a closing script tag anywhere inside, then it won't get exported correctly. The results won't be pretty, but this should take care of 90% of your code, and you should be able to clean up any problem results manually. There, does that help?

David McEwing

Basically you need to create a class file. Inherit the class from System.Web.UI.Page and then change the page directive of 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 the CodeBehind is the code file you just created. You might need to reload the project to get the solution explorer to display the file nested, but even if you don't it should work.

You can also check out the accepted answer to for an alternative. How does IIS know if it's serving a Web Site or a Web Application project?

I don't know of a shortcut way to be honest.

Your probably best bet is to create a new page and copy paste across until everything works, then delete your source, rename your new file to the old name and rebuild.

Not ideal, but probably the quickest/cleanest/safest way to port over.

Thanks alot! Here is a slighlty modified version if your code is written i VB.Net. Just compile and run the converter in every folder that contains 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();
                    }
                }
            }
        }

    }
}

if your aspx file has 2 sections and your are able to split it in a mechanical way why you don't write just a small parser to automate the work? Shouldn't be hard, it's just plain text manipulation and a recursive file find.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!