代码之家  ›  专栏  ›  技术社区  ›  Michael La Voie Frederik Gheysels

将单个aspx文件转换为后台代码

  •  7
  • Michael La Voie Frederik Gheysels  · 技术社区  · 16 年前

    我正在用VS 2008开发一个网站(不是web应用程序)。Net 3.5,它使用单文件.aspx模型,其中服务器代码包含在html的头部,而不是使用.aspx.cs代码隐藏页。

    我想快速转换文件以使用代码隐藏模型,但到目前为止,我唯一能做到这一点的方法是删除文件,创建一个同名的新的代码隐藏aspx页面,然后手动将aspx相关代码复制到.aspx页面,将服务器代码复制到.aspx.cs页面。

    回答这个问题,但不幸的是不要: Working with Single-File Web Forms Pages in Visual Studio .NET How do you convert an aspx or master page file to page and code behind?

    5 回复  |  直到 8 年前
        1
  •  19
  •   The Digital Gabeg    16 年前

    如果手动转换太耗时,而自动转换不起作用,我认为你唯一的其他选择就是构建自己的转换器。你可以编写一个简单的控制台应用程序,它在命令行上获取目录路径,并处理该目录中的每个文件。这并不难——在这里,我会让你开始:

    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分钟编码,30分钟调试。有一些明显的错误,比如,如果你的代码在任何地方都包含一个结束脚本标签 内部 ,则无法正确导出。结果不会很好,但这应该能处理90%的代码,你应该能够手动清理任何问题结果。这有帮助吗?

        2
  •  3
  •   Community CDub    8 年前

    基本上,您需要创建一个类文件。从System继承类。网状物。UI。Page,然后更改页面的Page指令以指向后面的代码。

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

    其中Inherits是类文件的名称,CodeBehind是您刚刚创建的代码文件。您可能需要重新加载项目以使解决方案资源管理器显示嵌套的文件,但即使您不这样做,它也应该工作。

    您还可以查看已接受的答案以获取替代方案。 How does IIS know if it's serving a Web Site or a Web Application project?

        3
  •  2
  •   Serapth    16 年前

    说实话,我不知道有什么捷径。

    您可能最好的选择是创建一个新页面并复制粘贴,直到一切正常,然后删除源文件,将新文件重命名为旧名称并重新生成。

    不理想,但可能是最快/最干净/最安全的转运方式。

        4
  •  1
  •   Flatron    15 年前

    非常感谢!如果你的代码是用VB.Net编写的,这是一个稍作修改的版本。只需在包含aspx站点的每个文件夹中编译并运行转换器。

    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();
                        }
                    }
                }
            }
    
        }
    }
    
        5
  •  0
  •   Andrea Balducci    16 年前

    如果你的aspx文件有2个部分,并且你能够以机械的方式分割它,为什么你不编写一个小型解析器来自动化这项工作呢?应该不难,这只是纯文本操作和递归文件查找。