⚠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을 전송하는 과정을 다룰 예정.
'C# > 근웹 연대기' 카테고리의 다른 글
c#으로 근본 없는 웹서버 개발기 29 : cshtml - html로 전송 (0) | 2022.02.03 |
---|---|
c#으로 근본 없는 웹서버 개발기 27 : cshtml 클래스 파일 만들기 (0) | 2022.01.30 |
c#으로 근본 없는 웹서버 개발기 26 : cshtml 파싱 - 코드 조립 (0) | 2022.01.27 |
c#으로 근본 없는 웹서버 개발기 25 : cshtml 파싱 - 파스노드(코드) (0) | 2022.01.24 |
c#으로 근본 없는 웹서버 개발기 24 : cshtml 파싱 - 파스노드 공통함수 (0) | 2022.01.22 |