在 ASP.NET Core 中替换 @helper [英] Replacement for @helper in ASP.NET Core

查看:21
本文介绍了在 ASP.NET Core 中替换 @helper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我认为 ViewComponent 并没有解决 TagHelper 的问题.有什么替代品吗?接受参数并返回 HtmlString 的东西?

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

我没有看到任何有害的:

I don't see anything harmful with:

@helper foo(string something) {
     <div>Say @something</div>
}

var emailbody = classfilenameinAppCodefolder.foo("hello"); //store result in a variable for further processes

现在我相信它是在 RC 之前的临时删除.https://github.com/aspnet/Razor/issues/281https://github.com/aspnet/Mvc/issues/1130 好吧!最好是.我希望有人在努力.如果没有 @helper,构建大型 HtmlString 或模板"将非常痛苦.

For now i believe its a temporary delete before RC. https://github.com/aspnet/Razor/issues/281 and https://github.com/aspnet/Mvc/issues/1130 Well! it better be. I hope someone is working on it. Without @helper, building large HtmlString or 'template' would be a serious pain.

注意:局部视图似乎无法解决问题.我认为它只呈现视图而不是将视图返回到变量.

Note: Partial View doesn't seem to do the trick. I think it only renders views not return view to variable.

其次,App_Code 文件夹怎么了?

Secondly, what happened to the App_Code folder?

推荐答案

根据下面的 Github issue,看来@helper 又回来了,将被包含在 asp .net core 3.0.0 preview 4 中.

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

更新

从 asp .net core 3 开始,您现在可以在 Razor 代码块中定义本地函数.

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

您也可以像这样使用@functions 指令:

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

这篇关于在 ASP.NET Core 中替换 @helper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆