In ASP.NET Webform, App_Code
is standard folder to putting code and using it at run-time.But I think this folder is kind of different in ASP.NET MVC, my question is:
where should I put my code ( Extension methods , Helpers , ... ) in ASP.NET MVC. When I store code in App_Code
folder, I can't use theme in controller
but they work fine in views
.
About Entity Framework, the same question, where should I put edmx and tt files. I'm not using Code-First
Update:
After some search, finally I created a new Class Library project in my solution, code is available in all controllers
and views
. I still don't know why the code in App_Code
is not available in controller
App_Code folder Contains source code files. The code is compiled as part of your application and is referenced automatically. The App_Code folder works much like the Bin folder, except that you can put source code in it instead of compiled code.
Area allows us to partition the large application into smaller units where each unit contains a separate MVC folder structure, same as the default MVC folder structure. For example, a large enterprise application may have different modules like admin, finance, HR, marketing, etc.
Add MVC Area with Visual StudioIn Solution Explorer, right click the project and select ADD > New Scaffolded Item, then select MVC Area. Areas are an ASP.NET feature used to organize related functionality into a group as a separate namespace (for routing) and folder structure (for views).
I had the same issue, my class Utility.cs was not recognized inside my MVC project. I changed the Build Action "Content" to "Compile" and that solved my problem. Hope that helps.
App_Code is necessary in Web Site projects because it has a special meaning. It means "Don't serve these files to a web browser". In ASP.NET MVC, files are not directly served to the browser in most cases, so App_Code is not necessary. You can place code files whereever you want, in any folder you want because these files are compiled into a DLL and not typically published to the website itself.
Using a stand-alone library is also a fine solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With