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

将单个文件aspx转换为代码隐藏

  •  6
  • Michael La Voie Frederik Gheysels  · 技术社区  · 15 年前

    我在vs 2008.net 3.5中的一个网站(不是Web应用程序)上工作,它使用单文件.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?

    这两种方法都提供了一个简单的解决方案,让vs完成腿的工作,你只要把它指向一个文件并拍摄。不管什么原因,他们都不工作。第一篇文章似乎提到了VS2002,第二篇文章似乎提到了Web应用程序。

    有没有希望建立一个网站?

    另外,也许我认为这是错误的,单页模型有什么优势吗?我确实计划很快将整个网站转换为Web应用程序,单页模型在Web应用程序中是否工作良好?

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

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

    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    7 年前

    基本上,您需要创建一个类文件。继承system.web.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    15 年前

    我不知道诚实的捷径。

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

    不太理想,但可能是最快/最干净/最安全的进出港方式。

        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    15 年前

    如果您的ASPX文件有两个部分,并且能够以机械方式拆分它,那么为什么不只编写一个小的解析器来自动化工作呢?不应该很难,它只是简单的文本操作和递归的文件查找。