Develop Dairy/C#

Visual studio code(VSC) IDE๋กœ Blazor WebAssembly ์‚ดํŽด๋ณด๊ธฐ (2021. 03. 17)

ํ‰์ƒ์ฝ”๋”ฉ 2021. 3. 17. 23:22

VSC = Visual studio code

๐Ÿ“–VSC๋กœ Blazor WebAssembly project ์—ด์–ด์„œ ์„ธํŒ…ํ•˜๊ธฐ

  1. ๋ช…๋ น์–ด์—์„œ ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ํด๋”๋กœ ๊ฐ„๋’ค VSC๋กœ ์—ด๊ธฐ
  2. ์ฒ˜์Œ์œผ๋กœ VSC๋ฅผ ์—ด์–ด๋ณด๋ฉด Blazor WebAssembly ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„ , ์˜ค๋ฅธ์ชฝ ์•„๋ž˜์— ๋œจ๋Š”

    "Yes"๋ฅผ ๋ˆŒ๋Ÿฌ ์ฃผ์‹œ๊ณ , ๋˜ ํ™•์žฅ(Extension)์„ ์„ค์น˜ ํ•ด์ค์‹œ๋‹ค. C#, JavaScript debugger ์ž…๋‹ˆ๋‹ค.

  3. ์ดํ›„์—๋Š” ์ด์ „ ์ฝ”์Šค (Blazor ํ˜ธ์ŠคํŒ… ํ™˜๊ฒฝ ์‹œ์ž‘ํ•˜๊ธฐ) ์—์„œ ๋นŒ๋“œ ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ

    VSC์—์„œ๋„ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    "Ctrl + ~"๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ dotnet build๋ฅผ ์ž…๋ ฅ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  4. "Server" ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ์ด๋™ํ•˜์—ฌ "dotnet run" ๋ช…๋ น์–ด ์‹คํ–‰์œผ๋กœ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ ํ•  ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.
    ๊ฟ€ํŒ: ํ•ด๋‹นํด๋” ๊ฒฝ๋กœ์—์„œ ์•ž์— cmd๋ฅผ ๋ถ™์—ฌ๋ณด๊ธฐ

 

๐Ÿ“–Blazor wasm(web assembly)

Blazor WebAssembly์˜ ์ง„์ž…์  - Client/Program.cs ์˜ main ํด๋ž˜์Šค๊ฐ€ ์ง„์ž…์ .

 

1. ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ ํ•˜๊ธฐ ์ „์— ๋ฃจํŠธ ์ถ”๊ฐ€&DOM ์š”์†Œ์˜ ์ด๋ฆ„์„ ์ง€์ •

builder.RootComponents.Add<App>("#app");

์š”์†Œ์˜ ์ด๋ฆ„์„ wwwroot/index.html์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

<div id="app">Loading...</div>

์š”์†Œ์˜ ์ด๋ฆ„์„ ๋‹ค๋ฅธ๊ฒƒ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„ ๋‘๊ฐœ์˜ app์ด๋ผ๊ณ  ์ ํ˜€์žˆ๋Š”๋ถ€๋ถ„์„

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ ์ณ๋ณด์ž.

builder.RootComponents.Add<App>("#CuriousDrive");
<div id="CuriousDrive">Loading...</div>

[chrome F12๊ฐ€ ์ผœ์ ธ์žˆ๋Š” ์‚ฌ์ง„]

 

img

์š”์†Œ์˜ ์ด๋ฆ„์ด ๊ธฐ์กด app ์—์„œ CuriousDrive๋กœ ๋ฐ”๋€Œ์–ด ์žˆ๋‹ค.

 

๋ฃจํŠธ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์–ด๋””์—์„œ ์—ฌ๋‚˜์š”? (๋ผ์šฐํ„ฐ)

img


์œ„ ๊ทธ๋ฆผ ํŒŒ์ผ ๊ฐ™์ด Project/Client/App.razor ์š”์†Œ์— ๋ผ์šฐํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />

์—์„œ

 

2. MainLayout์€ Client/Shared/Mainrayout.razor์—์„œ ์ฐพ์•„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜๋Š” Mainrayout.razor ์ž…๋‹ˆ๋‹ค.

img

 

3. NavMenu๋Š” Shared/NavMenu.razor์—์„œ ์‚ดํŽด ๋ณผ ์ˆ˜์žˆ๊ณ , ๊ทธ ์•ˆ์—

 

img

 

NavLink ํƒœ๊ทธ ์•ˆ์— herf="" ๋Š” Client/Pages/Index.razor ์—์„œ ๋ฃจํŠธ์ฃผ์†Œ @page "/" ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

href="counter"๋Š” Pages์—์„œ Counter.razor ์—์„œ @page "/counter"๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

4. FetchData.razor์—์„œ๋Š” WeatherForecast ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š”๋ฐ

 

VSC ๋‹จ์ถ•ํ‚ค์ธ Ctrl + P ๋ฅผ ํ™œ์šฉํ•˜์—ฌ "WeatherForecast" ๋ฅผ ๊ฒ€์ƒ‰ํ•ด์„œ ์ฐพ์•„๋ณด๋ฉด

 

WeatherForecastController.cs๊ฐ€ ๋ณด์ด๋Š”๋ฐ ๊ฒฝ๋กœ๋Š” Project/Server/Controllers ์— ์žˆ๋‹ค.

 

localhost:5001/WeatherForecast ์—์„œ Json ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธ ๊ฐ€๋Šฅ ํ•˜๋‹ค.

img