23 继续讲 Tag Helpers 和复习 View Component.mp4 (54.41MB)

条件 Tag Helpers

ConditionTagHelper:

  1. [HtmlTargetElement(Attributes = nameof(Condition))]
  2. public class ConditionTagHelper : TagHelper
  3. {
  4. public bool Condition { get; set; }
  5. public override void Process(
  6. TagHelperContext context,
  7. TagHelperOutput output)
  8. {
  9. if (!Condition) output.SuppressOutput();
  10. }
  11. }

使用 ConditionTagHelper:

  1. <li class="nav-item" condition="SignInManager.IsSignedIn(User)">
  2. <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>
  3. </li>
  4. <li class="nav-item" condition="SignInManager.IsSignedIn(User)">
  5. <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })">
  6. <button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
  7. </form>
  8. </li>
  9. <li class="nav-item" condition="!SignInManager.IsSignedIn(User)">
  10. <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>
  11. </li>
  12. <li class="nav-item" condition="!SignInManager.IsSignedIn(User)">
  13. <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
  14. </li>

Tag Helpers 前缀

通过 Tag Helpers 前缀避免名称冲突。

以 “my:” 作为 Tag Helpers 前缀:

  1. @tagHelperPrefix "my:"
  2. ...
  3. <ul class="navbar-nav">
  4. <li class="nav-item" condition="SignInManager.IsSignedIn(User)">
  5. <my:a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</my:a>
  6. </li>
  7. <my:li class="nav-item" condition="SignInManager.IsSignedIn(User)">
  8. <my:form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })">
  9. <button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
  10. </my:form>
  11. </my:li>
  12. ...
  13. </ul>

View Component

  • 显示部分内容
  • Mini Controller
  • 可复用
  • 只能配合父级 View 使用

建立 View Component:

  • 继承 ViewComponent 父类
  • 实现 Invoke() 方法
    • 返回 IViewComponentResult
  • 或实现 InvokeAsync() 方法

    • 返回 Task

      View Component 实例

      InternetStatus:

      1. public class InternetStatus : ViewComponent
      2. {
      3. public async Task<IViewComponentResult> InvokeAsync()
      4. {
      5. var httpClient = new HttpClient();
      6. var response = await httpClient.GetAsync("https://www.baidu.com");
      7. return View(response.StatusCode == HttpStatusCode.OK);
      8. }
      9. }

View Component 的 View:

  1. @model bool
  2. @if (Model)
  3. {
  4. <span class="badge badge-success">Connected</span>
  5. }
  6. else
  7. {
  8. <span class="badge badge-danger">Not Connected</span>
  9. }

在 _Layout 中进行使用:

  1. <div class="container">
  2. <partial name="_CookieConsentPartial" />
  3. <main role="main" class="pb-3">
  4. @RenderBody()
  5. @await Component.InvokeAsync("InternetStatus")
  6. </main>
  7. </div>

注:View Component 只有放在 Shared 目录下才能全局可用。