ASP.NET MVC 4 custom HTML Helpers folder location

我只是一个虾纸丫 提交于 2019-12-12 12:00:34

问题


I'm starting to develop an application using ASP.NET MVC 4 framework with Razor syntax. I want to know where (folder location) I should create my HTML Helper class. Best practice.

For example:

  • VisualStudioSolution
    • Controlles
    • Html
      • HtmlHelperClass.vb
    • Models
    • Views

回答1:


use this.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!")



回答2:


You have a good structure.

I would change the Html folder with a utility folder. You can add all kinda helpers there.

  • Controllers
  • Models
  • Views
  • Utility
  • Framework (this may be usefull for the bootstrapping of your app)

And there actually no fix "best practice". Just make sure you can find your classes in the obvious places. If not remodel.



来源:https://stackoverflow.com/questions/14582243/asp-net-mvc-4-custom-html-helpers-folder-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!