본문 바로가기

C#/근웹 연대기

c#으로 근본 없는 웹서버 개발기 28 : cshtml - 빌드 및 dll

⚠WARNING
ASP.NET에 대한 포스팅이 아닙니다
나가실 문은 오른쪽 하단입니다

 

 

cshtml 파일을 cs로 만들면 대략... 아래와 같이 된다

(예시는 Layout 페이지로 어떤 페이지가 자리잡기 위한 하나의 프레임이 된다)

 

변환 전

<head>
    <title> @ViewData["title"] </title>
</head>
<body>
@{
    string p = "Layout Page";
    <p> @p </p>
    <div> @RenderBody() </div>
    @RenderSection("testsec");
    <script>
        console.log("ㅇㅇ");
    </script>
}
</body>

 

변환 후

using dotweb;

namespace Views;
//--------------------------------------------
// <auto-generated>                          
//     This code was generated by a tool.    
// </auto-generated>                          
//--------------------------------------------
public class Views_cshtml_layout : View
{
    public Views_cshtml_layout(Response res, JO ViewData, dynamic ViewBag, TempJO TempData) :
        base(res, ViewData, (object)ViewBag, TempData) { }
    public Views_cshtml_layout(View child) : base(child) { }

    public override string GetHTML()
    {
        Layout = null;
W(@"<head>
    <title> "+ViewData["title"]+@" </title>
</head>
<body>
");
    string p = "Layout Page";
    W(@"<p> "+p+@" </p>");
    W(@"<div> "+RenderBody()+@" </div>");
    W(RenderSection("testsec"));;
    W(@"<script>
        console.log("ㅇㅇ");
    </script>");
W(@"
</body>
");
        if (Layout == null) return GetW();
        var type = Type.GetType("Views." + GetClassName());
        return (Activator.CreateInstance(type, this) as View).GetHTML();
    }
}

W는 하나의 함수로 브라우저에 전송할 HTML을 만든다.

 

 

첫 작업은 변환된 내용을 다음과 같이 파일로 저장하는 것이다.

    public string MakeSource(FileInfo info)
    {
        dir ??= new DirectoryInfo(info.Directory.FullName);

        Console.WriteLine(info.FullName);

        var txt = File.ReadAllText(info.FullName, Encoding.UTF8);

        List<string> tokens = Tokenize(txt);


        var node = new InTagNode(tokens);

        var classNm = info.FullName[(Util.projPath.Length + 1)..];
        classNm =  View.GetClassName(classNm);

        var rt = Node2ViewClass(classNm, node);

        var fname = Path.GetFileName(info.FullName);

        File.WriteAllText(Util.projPath + "/Views/out/" + fname + ".cs", rt, Encoding.UTF8);

        return rt;
    }

이전 포스팅에서 FIle.WriteAllText 가 추가되었다.

 

 

두 번째는 그 파일들을 컴파일하여 dll을 로드하는 것이다.

    public void InitCSHTML()
    {
        new TemplateEngine().MakeSourceAll(Util.projPath + "/Views");

        var ps = Process.Start(new ProcessStartInfo()
        {
            FileName = "dotnet",
            Arguments = "build --source .",
            WorkingDirectory = Util.projPath + "/Views"
        }); ;
        ps.WaitForExit();
        if (ps.ExitCode != 0) throw new Exception("Compile Error.");

        var asm = Assembly.LoadFile(Util.projPath + @"\Views\bin\Debug\net6.0\Views.dll");
        viewAsm = asm ?? throw new ArgumentException("Can't find Views.dll");
    }

 

 

컴파일이 정상적으로 된다면 아래와 같은 화면이 나온다.

빌드했지비.

 

 

다음 포스팅은 컴파일된 dll을 이용하여 브라우져에 html을 전송하는 과정을 다룰 예정.