使用MVC 4:以任何方式实现以下内容,而不是在每个使用它的视图中都使用@helper
- 部分内联:
@helper _(string msg) {
@msg
}
<p>@_("Hello")</p>
...那么布局文件有它还是放在其他共享的地方?即仅在给定视图中显示:@_("Hello")
。
我试过了:
Views/Shared/_Layout.cshtml
(不起作用)App_Code
(Works但是已命名空间)我知道我可以使用命名空间,但我真的喜欢为了可读性和更短的代码而避免使用它。
我对.NET仍然相对较新,所以任何帮助都会受到赞赏。
答案 0 :(得分:1)
这可以在不创建帮助程序的情况下解决。使用扩展方法。
namespace WebApplication1
{
public static class WebViewPageExtensions
{
public static IHtmlString _(this WebViewPage page, string msg)
{
return new HtmlString(msg);
}
}
}
然后在你的所有观点中:
<p>@_("Hello")</p>