Try   HackMD

VS Code 對 .Net 的 .cshtml 格式化及程式解析

tags: .Net 筆記

使用 VS 的 cshtml 裡開發 js,在閱讀上是有一點不太容易,這篇做一個筆記

以下用 Vuejs 舉例,會看到都是只有白色的一片

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

假如要有良好的 Color 去識別程式碼,就需要將它們抽離並放入 .js 裡面

但假設這段的程式碼裡面會有需要 "@" 的 .Net 語法就會有有一些實務上整合的麻煩。

如下圖的例子,如:多語系、url 的產生

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

在網路上找了解決辦法,大部分是提到說放入 window 底下,做全域變數整合

以上 razor 與純 js 的問題,就是另一個題外話了,未來有機會再做處理

相比 VS 的程式碼閱讀,VS Code 在這一塊就很好

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

雖然 VS 2022 17 版官方已經有說明會加強這塊。

短時間要完善可能還要一大段,目前還是以 VS Code 協助 cshtml 的開發

VS Code 要設定這一塊會有幾個步驟

出處

VS Code 的使用者設定內
{
  "editor.formatOnSave": true,
  "emmet.includeLanguages": {
    "aspnetcorerazor": "html" // 重要,告訴編譯器 html 內有 razor
  },
  "files.associations": {
    "*.cshtml": "html"
  }
}

如此在存檔時,也能夠同時做 format 但其實也變相鼓勵在 VS Code 開發 .Net (笑

之後在 Mac OS 可能也比較可以無痛銜接