7

VS 2008 .Net 3.5のWebサイト(Webアプリケーションではない)で作業しており、.aspx.csを使用する代わりに、サーバーコードがhtmlのヘッド部分に含まれている単一ファイルの.aspxモデルを使用しています。ページの後ろのコード。

コードビハインドモデルを使用するようにファイルをすばやく変換したいのですが、これを行う唯一の方法は、ファイルを削除し、同じ名前の新しいコードビハインドaspxページを作成してから、手動でコピーすることです。 aspx関連のコードを.aspxページに、サーバーコードを.aspx.csページに。

これを行うためのより速い方法はありますか?

この質問に答えているように見える2つの記事を見ましたが、残念ながらそうではありません 。VisualStudio .NETで の単一ファイルWebフォームページの操作と、aspxまたはマスターページファイルをページとコードビハインドに変換するにはどうすればよいですか。

どちらも、VSが脚の作業を行うという単純なソリューションを提供します。ファイルをポイントして、撮影するだけです。何らかの理由で、それらは機能していません。最初の記事はVS2002を参照しているようで、2番目の記事はWebアプリケーションを参照しているようです。

ウェブサイトに希望はありますか?

また、これは間違った見方をしているかもしれませんが、シングルページモデルには利点がありますか?Webサイト全体をすぐにWebアプリケーションに変換する予定ですが、単一ページモデルはWebアプリケーションでうまく機能しますか?

4

5 に答える 5

19

手動の変換に時間がかかりすぎて、自動変換が機能しない場合は、独自のコンバーターを作成する以外に唯一の選択肢があると思います。コマンド ラインでディレクトリ パスを取得し、そのディレクトリ内のすべてのファイルを処理する単純なコンソール アプリを作成できます。これはそれほど難しいことではありません。ここから始めましょう。

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% が処理され、問題のある結果を手動でクリーンアップできるはずです。ほら、役に立ちますか?

于 2009-05-18T22:35:55.437 に答える
3

基本的に、クラスファイルを作成する必要があります。System.Web.UI.Page からクラスを継承し、ページのページ ディレクティブを変更してコード ビハインドを指すようにします。

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

Inherits はクラス ファイルの名前で、CodeBehind は作成したばかりのコード ファイルです。ソリューション エクスプローラーでネストされたファイルを表示するには、プロジェクトを再読み込みする必要がある場合がありますが、そうしなくても機能するはずです。

代替案については、受け入れられた回答を確認することもできます。IIS は、Web サイトまたは Web アプリケーション プロジェクトを提供しているかどうかをどのように認識しますか?

于 2009-05-12T20:27:40.540 に答える
2

正直なところ、ショートカットの方法はわかりません。

おそらく最善の策は、新しいページを作成し、すべてが機能するまでコピーペーストしてから、ソースを削除し、新しいファイルの名前を古い名前に変更して再構築することです。

理想的ではありませんが、おそらく最も迅速でクリーンで安全な移植方法です。

于 2009-05-12T19:59:10.493 に答える
1

どうもありがとう!あなたのコードが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();
                    }
                }
            }
        }

    }
}
于 2009-11-20T12:26:50.530 に答える
0

aspx ファイルに 2 つのセクションがあり、それを機械的に分割できる場合、作業を自動化する小さなパーサーだけを作成しないのはなぜですか? 難しいことではありません。単純なテキスト操作と再帰的なファイル検索だけです。

于 2009-05-15T21:42:10.633 に答える