Replacement for @helper in ASP.NET Core

后端 未结 8 1565
傲寒
傲寒 2020-11-30 06:35

So far, i don\'t think ViewComponent solves that neither does TagHelper. Is there any replacement to this? Something that takes parameters and retu

8条回答
  •  情话喂你
    2020-11-30 07:05

    You can easily replace that "feature" with a ViewComponent (and a TagHelper if you want). ASP.NET Core is much more friendly to web designers, and the ViewComponents allow you to write HTML without any (weird to most) razor code.

    For example:

    1. Create a SayComponent : ViewComponent class:

      public class SayComponent : ViewComponent
      {
          public void Render(string message)
          {
              return View(message);
          }
      }
      
    2. Create a View file under Views/Shared/Say/Default.cshtml with just

      @model string
      
      
      Message: @Model.
    3. And call it:

      @await Component.RenderAsync("Say", "some message")
      

    For a better experience, add this to your _ViewImports.cshtml file:

    @addTagHelper *, YourSolutionName
    

    And then you can use it as a tag helper:

    
    

提交回复
热议问题