Replacement for @helper in ASP.NET Core

后端 未结 8 1563
傲寒
傲寒 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: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)
        {
            

    Name: @name

    } 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)
        {
            

    Name: @name

    } }

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

提交回复
热议问题