Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 and App_Code folder

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

like image 398
Mironline Avatar asked May 16 '12 14:05

Mironline


People also ask

What is the App_Code folder?

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.

What is MVC Area folder?

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.

How can add area folder in MVC?

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).


2 Answers

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.

like image 156
polykos Avatar answered Oct 15 '22 05:10

polykos


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.

like image 41
Erik Funkenbusch Avatar answered Oct 15 '22 07:10

Erik Funkenbusch