在 .Net Core Razor 页面上显示文本标签的最佳实践

Best Practice to display text label on a .NetCore Razor page

将 .Net Core Razor 网页上的 ALL 文本注入模型绑定(即使在静态页面上)是最佳做法,还是我应该只注入文本可能需要在运行时动态更改?

例如我的 Index.cshtml 页面有一个 h1 标题,如下所示。这被认为是不好的做法还是可以?

<h1 class="block-title-text" localize-content>A Fun Title</h1>

谢谢。只是想了解一下 Razor 和 .Net Core。

如果没问题,通常您应该将用户界面元素保留在视图中(或 Razor Pages 的 .cshtml 文件)。有关详细信息,请参阅 benefits of using views,其中包括:

The parts of the app are loosely coupled. You can build and update the app's views separately from the business logic and data access components. You can modify the views of the app without necessarily having to update other parts of the app.

刚刚意识到事后看来很明显的事情。通过使用模型绑定注入字符串而不是像上面那样将它们放入 HTML 中,它允许围绕这些字符串合并单元测试。