在 ASP.NET MVC 的 _ViewStart 中定义@helper?
是否可以在 _ViewStart.cshtml 中定义共享 @helper,以便它可在其目录中的所有视图中使用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以在 _ViewStart.cshtml 中定义共享 @helper,以便它可在其目录中的所有视图中使用?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
不,在 _ViewStart 中定义 @helper 不起作用,但您可以为共享助手创建一个新的 Razor 视图并将其放置在 App_Code 文件夹中。一个小缺点是必须在与视图同名的类型上以静态方法的形式调用帮助器,这使得该技术变得更加冗长。
这是一个示例:
视图中的帮助程序方法位于此处:~/App_Code/RazorHelpers.cshtml:
视图中的帮助程序用法:
请参阅此SO问题:应用程序代码中的 Razor 助手文件夹
No, defining the @helper in _ViewStart will not work, but you can create a new Razor view for shared helpers and place it in the App_Code folder. One minor drawback is the helper must be called as a static method on a type with the same name as the view making this technique a little more verbose.
Here is an example:
Helper Method in View located here: ~/App_Code/RazorHelpers.cshtml:
Helper Usage in a View:
See this SO Question: Razor Helper In App Code Folder
您可以将助手添加到单独的文件中,并且它将可供您的所有视图使用。请参阅 Scoot Gu 的帖子:http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx
You can add the helper in a separate file and it will be available to all your views. See the Scoot Gu's post about it: http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx