在 ASP.NET Core 中替换 @helper

编程入门 行业动态 更新时间:2024-10-28 19:22:33
本文介绍了在 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 之前的临时删除.github/aspnet/Razor/issues/281 和 github/aspnet/Mvc/issues/1130 好吧!最好是.我希望有人在努力.如果没有 @helper,构建大型 HtmlString 或模板"将非常痛苦.

For now i believe its a temporary delete before RC. github/aspnet/Razor/issues/281 and github/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 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 core 3.0.0 preview 4.

github/aspnet/AspNetCore/issues/5110

更新

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

Starting in asp 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."); }

docs.microsoft/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> } }

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

更多推荐

在 ASP.NET Core 中替换 @helper

本文发布于:2023-11-04 12:40:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557969.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   helper   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!