ASP.NET MVC 4 custom HTML Helpers folder location
Asked Answered
I

2

5

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
Insensible answered 29/1, 2013 at 11:51 Comment(0)
I
7

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!")
Inscription answered 29/1, 2013 at 12:8 Comment(3)
I ran into a problem deploying to a hosting provider that did not allow App_Code folder, so am currently unable to use these helpers at all. https://mcmap.net/q/2035401/-how-to-use-asp-net-mvc-view-precompilation-with-app_code-helpersAgribusiness
@SeanMill you can add this at the top of your view "@using UtilsProject.Helpers" and to use your helper "@Html.MyCustomHelper()". At my application I add new Project to solution called UtilsProject and inside of that I have my custom helper.Insensible
Not sure if have using in any view is really good idea, wasn't this why we have been using extension on Html ?Idden
C
0

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.

Carpio answered 29/1, 2013 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.