Replacement for @helper in ASP.NET Core

后端 未结 8 1545
傲寒
傲寒 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 06:43
    @{
        Func<String, IHtmlContent> foo = @<div>Say @item</div>;
    }
    
    0 讨论(0)
  • 2020-11-30 06:46

    The @helper directive was removed since it was incomplete and its current design did not fit in the new 'ASP.NET 5 way'. One of the reasons is that helpers should be declared in the App_Code folder while ASP.NET 5 has no concept of special folders. Therefore the team decided to temporarily remove the feature.

    There are plans to bring it back in the future though. See this and this.

    0 讨论(0)
  • 2020-11-30 06:47

    How about using partials to recreate reusable tags?

    MyProject/Views/Shared/_foo.cshtml

    @model string
    
    <div>@Model</div>
    

    MyProject/Views/Courses/Index.cshtml

    @{
        Layout = "_Layout";
    }
    
    <div>
       <partial name="_foo" model="foo" />
       <partial name="_foo" model="bar" />
       <partial name="_foo" model="baz" />
    </div>
    
    0 讨论(0)
  • 2020-11-30 06:59

    For .NET Core 3, you can use local functions:

    @{
        void RenderName(string name)
        {
            <p>Name: <strong>@name</strong></p>
        }
    
        RenderName("Mahatma Gandhi");
        RenderName("Martin Luther King, Jr.");
    }
    

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-3.1#razor-code-blocks

    0 讨论(0)
  • 2020-11-30 07:04

    According to the following Github issue, it looks like @helper is coming back and will be included in asp .net core 3.0.0 preview 4.

    https://github.com/aspnet/AspNetCore/issues/5110

    UPDATE

    Starting in asp .net core 3, you can now define a local function within a Razor code block.

    @{
        void RenderName(string name)
        {
            <p>Name: <strong>@name</strong></p>
        }
    
        RenderName("Mahatma Gandhi");
        RenderName("Martin Luther King, Jr.");
    }
    

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-3.1#razor-code-blocks

    Alternatively you can use the @functions directive like this:

    @{
        RenderName("Mahatma Gandhi");
        RenderName("Martin Luther King, Jr.");
    }
    
    @functions {
        private void RenderName(string name)
        {
            <p>Name: <strong>@name</strong></p>
        }
    }
    

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-3.1#functions

    0 讨论(0)
  • 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
      
      <div>Message: @Model.</div>
      
    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:

    <vc:say message="some message"></vc:say>
    
    0 讨论(0)
提交回复
热议问题