Where should I place Declarative HTML helpers in ASP.NET MVC 3

后端 未结 1 1342
庸人自扰
庸人自扰 2020-12-17 16:04

I\'m trying to make a declarative HTML Helper as specified in ScottGu\'s Razor post, but I\'m not having much luck. I tried putting a Helpers.cshtml file with a DateTimeHelp

相关标签:
1条回答
  • 2020-12-17 16:43

    To use the "@helper" feature in Razor you need to place the CSHTML file in the App_Code folder of your app. There is no "Views/Helpers" folder in ASP.NET MVC 3. ScottGu's blog post was written before the feature was fully implemented, and some of the notes there are not entirely accurate anymore.

    To call the "@helper" that you wrote you have to include both the filename as well as the name of the helper inside it. For example, if you have this helper:

    ~/App_Code/MyHelper.cshtml

    And this content:

    @helper ShowStuff(string stuff) {
        <p>@stuff</p>
    }
    

    Then you call it like so:

    @MyHelper.ShowStuff("some stuff!")
    
    0 讨论(0)
提交回复
热议问题