如何在 cshtml 模板中创建函数? [英] How to create a function in a cshtml template?
本文介绍了如何在 cshtml 模板中创建函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一个只需要在一个 cshtml 文件中的函数.您可以将我的情况视为 ASP.NET 页面方法,它们是在页面中实现的最小 Web 服务,因为它们的范围仅限于一个页面.我知道 HTML 助手(扩展方法),但我的函数只需要在一个 cshtml 文件中.我不知道如何在视图中创建函数签名.注意:我使用的是 Razor 模板引擎.
I need to create a function that is only necessary inside one cshtml file. You can think of my situation as ASP.NET page methods, which are min web services implemented in a page, because they're scoped to one page. I know about HTML helpers (extension methods), but my function is just needed in one cshtml file. I don't know how to create a function signature inside a view. Note: I'm using Razor template engine.
推荐答案
您可以使用@helper Razor 指令:
You can use the @helper Razor directive:
@helper WelcomeMessage(string username)
{
<p>Welcome, @username.</p>
}
然后你像这样调用它:
@WelcomeMessage("John Smith")
这篇关于如何在 cshtml 模板中创建函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文