문제

vs 2008 .NET 3.5의 웹 사이트 (웹 응용 프로그램이 아님)에서 작업 중이며 .aspx.cs를 사용하는 대신 서버 코드가 HTML의 헤드 부분에 포함 된 단일 파일 .aspx 모델을 사용합니다. 페이지 뒤에 코드.

Code-Behind 모델을 사용하기 위해 파일을 빠르게 변환하고 싶지만 지금 까지이 작업을 수행 할 수있는 유일한 방법은 파일을 제거하고 동일한 이름의 새 코드 베어드 ASPX 페이지를 작성한 다음 수동으로 복사하는 것입니다. .aspx 페이지의 ASPX 관련 코드와 서버 코드는 .aspx.cs 페이지로 향합니다.

더 빠른 방법이 있습니까?

나는 두 기사를 보았다 ~인 것 같다 이 질문에 답하기 위해, 불행히도 :Visual Studio .net에서 단일 파일 웹 양식 페이지로 작업 그리고ASPX 또는 마스터 페이지 파일을 페이지와 코드 뒤에 어떻게 변환합니까?

둘 다 다리가 다리가 작동하는 간단한 솔루션을 제공하면 파일을 가리키고 촬영합니다. 어떤 이유로 든 그들은 작동하지 않습니다. 첫 번째 기사는 vs 2002를 참조하고 두 번째 기사는 웹 응용 프로그램을 참조하는 것으로 보입니다.

웹 사이트에 대한 희망이 있습니까?

또한, 나는 이것을 잘못된 방식으로보고 있을지도 모른다. 단일 페이지 모델에 이점이 있습니까? 전체 웹 사이트를 웹 애플리케이션으로 곧 변환 할 계획입니다. 단일 페이지 모델이 웹 응용 프로그램에서 잘 작동합니까?

도움이 되었습니까?

해결책

수동 변환이 너무 시간 집약적이고 자동 변환이 작동하지 않으면 다른 옵션이 자신의 변환기를 구축하는 것입니다. 명령 줄에서 디렉토리 경로를 가져 와서 해당 디렉토리의 모든 파일을 처리하는 간단한 콘솔 앱을 작성할 수 있습니다. 이건 너무 어렵지 않아 - 여기서 시작하겠습니다.

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%를 처리해야하며 수동으로 문제 결과를 정리할 수 있어야합니다. 거기, 그게 도움이 되나요?

다른 팁

기본적으로 클래스 파일을 만들어야합니다. System.web.ui.page에서 클래스를 상속 한 다음 페이지의 페이지 지시문을 변경하여 코드 뒤의 코드를 가리 킵니다.

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

상속이 클래스 파일의 이름이고 CodeBehind는 방금 만든 코드 파일입니다. 솔루션 탐색기가 중첩 된 파일을 표시하도록 Solution Explorer를 가져 오려면 프로젝트를 다시로드해야 할 수도 있지만 작동하지 않더라도 작동하지 않아도됩니다.

대안에 대한 수락 된 답변을 확인할 수도 있습니다. IIS가 웹 사이트 또는 웹 응용 프로그램 프로젝트에 서비스를 제공하고 있는지 어떻게 알 수 있습니까?

솔직히 말하면 바로 가기 방법을 모릅니다.

가장 좋은 방법은 모든 것이 작동 할 때까지 새 페이지를 만들고 페이스트를 복사 한 다음 소스를 삭제하고 새 파일 이름을 기존 이름으로 바꾸고 재건하는 것입니다.

이상적이지는 않지만 아마도 가장 빠른/가장 깨끗하고 가장 안전한 방법 일 것입니다.

정말 감사합니다! 코드가 작성된 경우 Slighlty 수정 버전입니다. i 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();
                    }
                }
            }
        }

    }
}

ASPX 파일이 2 개의 섹션이 있고 작업을 자동화하기 위해 작은 구식 구식을 작성하지 않는 이유를 기계적인 방식으로 분할 할 수있는 경우? 어렵지 않아야합니다. 단지 평범한 텍스트 조작과 재귀 파일 찾기입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top