Razor: Declarative HTML helpers

2019-01-04 10:35发布

I'm trying to write a simple declarative html helper:

@helper Echo(string input) {
    @input
}

The helper works fine if I embed it into the page I want to use it on. But if I move it to a separate .cshtml file and place that file in the ~/Views/Helpers directory, my view can't be compiled anymore because the helper is not found. According to Scott Gu's blog article on Razor it should work.

What am I doing wrong?

7条回答
走好不送
2楼-- · 2019-01-04 11:31

As marcind said, we weren't able to support the ~/Views/Helpers location due to some limitations in our compilation model.

We're working on a better solution for declarative HTML helpers, but it won't make v1. For v1, we have two solutions:

  1. App_Code (which has some problems as Marcin mentioned, but does work)
  2. David Ebbo (member of the team) has a Visual Studio add-in that compiles them into your DLL
查看更多
登录 后发表回答