ASP。NET MVC项目和App_Code文件夹

时间:2021-07-21 08:10:11

How come App_Code is not a choice in the Add ASP.NET Folder submenu in the VS solution explorer? I realize you can create one yourself manually by just renaming a New Folder, but what is the rational here? Is this not where you are supposed to put "utility" or "service layer" type classes?

为什么App_Code不是Add ASP中的选项。NET文件夹子菜单在VS解决方案资源管理器?我知道你可以通过重命名一个新的文件夹来手动创建一个,但是这里的rational是什么呢?难道这不是您应该放置“实用程序”或“服务层”类型类的地方吗?

On a MVC project side note. I do like the fact that there is a reference to System.Configuration out-of-the-box unlike the default ASP.NET Web Form Projects.

在MVC项目的附注中。我确实喜欢有系统的参考。与默认的ASP不同,配置是开箱即用的。净Web表单项目。

4 个解决方案

#1


10  

The real answer is because Microsoft wants you to buy in that you need VS to do MVC. However, to convert the project to pure JIT which is way easier to work with...

真正的答案是,因为微软想让你购买你需要VS做MVC。但是,要将项目转换为纯JIT,这更容易处理……

You can do it pretty straight forward.

你可以直接做。

(1) Move all folders containing class files to App_Code (leave Views folder where it is)
(2) Make Global.asax not needing a code behind or inherits reference by removing those attributes then placing

(1)将所有包含类文件的文件夹移动到App_Code(留下视图文件夹)(2)使其成为全局的。asax不需要代码,或者通过删除这些属性来继承引用。

[script runat="server"] 
... place contents of code behind's class inner code 
[/script] 

and delete the code behind for good

并将代码永久删除。

(3) In Bin Remove the project DLL reference making sure to keep the other necessary DLLs in terms of running MVC
(4) Remove solution files and properties folder and obj folder

(3)在Bin中删除项目DLL引用,确保在运行MVC时保留其他必要的DLL(4)删除解决方案文件和属性文件夹和obj文件夹

I do this routinely for nop MVC as its much simpler to work with the web tier on JIT. Developers that don't understand this are not working in the trenches every day on many websites with various custom quick fixes needed. The sites we work on get hundreds thousands of hits per day so using JIT has no performance penalty after the initial bump.
Seems less is more when it comes to smaller website development.

对于nop MVC,我经常这样做,因为在JIT上使用web层要简单得多。不了解这一点的开发人员并不是每天都在许多需要各种定制快速修复的网站上工作。我们每天工作的站点每天都有成千上万的点击量,所以使用JIT在最初的bump之后没有性能损失。当涉及到更小的网站开发时,看起来越少越好。

#2


21  

This is probably because an ASP.NET MVC project is a web application rather than a web site. In a web site, class files are compiled dynamically at run-time and must live in the App_Code folder. In a web application, everything is compiled statically and class files can live anywhere in your web application.

这可能是因为ASP。NET MVC项目是一个web应用程序,而不是一个web站点。在web站点中,类文件在运行时是动态编译的,并且必须位于App_Code文件夹中。在web应用程序中,所有内容都是静态编译的,类文件可以驻留在web应用程序的任何位置。

As David Brown pointed out, it's generally recommended to put extra class definition in a separate class library and then reference that from your web application. If you write unit tests or reference your classes from config files, it can be challenging or impossible to access these classes if they are defined only within your web application.

正如David Brown指出的,一般建议将额外的类定义放在一个单独的类库中,然后从web应用程序中引用它。如果您编写单元测试或从配置文件中引用类,那么如果这些类仅在web应用程序中定义,那么访问这些类可能是困难的,也可能是不可能的。

#3


10  

Generally, I place "service layer" and "utility" classes in a separate project and add it as a reference to my web application. With the MVC framework, I don't really see a need to compile classes at runtime.

通常,我将“服务层”和“实用程序”类放在单独的项目中,并将其添加为对web应用程序的引用。使用MVC框架,我不认为需要在运行时编译类。

#4


0  

For those who don’t want a second project/class library and just want a simple folder for a few c# classes, I propose “App_Classes”.

对于那些不想要第二个项目/类库而只想要几个c#类的简单文件夹的人,我建议使用“App_Classes”。

#1


10  

The real answer is because Microsoft wants you to buy in that you need VS to do MVC. However, to convert the project to pure JIT which is way easier to work with...

真正的答案是,因为微软想让你购买你需要VS做MVC。但是,要将项目转换为纯JIT,这更容易处理……

You can do it pretty straight forward.

你可以直接做。

(1) Move all folders containing class files to App_Code (leave Views folder where it is)
(2) Make Global.asax not needing a code behind or inherits reference by removing those attributes then placing

(1)将所有包含类文件的文件夹移动到App_Code(留下视图文件夹)(2)使其成为全局的。asax不需要代码,或者通过删除这些属性来继承引用。

[script runat="server"] 
... place contents of code behind's class inner code 
[/script] 

and delete the code behind for good

并将代码永久删除。

(3) In Bin Remove the project DLL reference making sure to keep the other necessary DLLs in terms of running MVC
(4) Remove solution files and properties folder and obj folder

(3)在Bin中删除项目DLL引用,确保在运行MVC时保留其他必要的DLL(4)删除解决方案文件和属性文件夹和obj文件夹

I do this routinely for nop MVC as its much simpler to work with the web tier on JIT. Developers that don't understand this are not working in the trenches every day on many websites with various custom quick fixes needed. The sites we work on get hundreds thousands of hits per day so using JIT has no performance penalty after the initial bump.
Seems less is more when it comes to smaller website development.

对于nop MVC,我经常这样做,因为在JIT上使用web层要简单得多。不了解这一点的开发人员并不是每天都在许多需要各种定制快速修复的网站上工作。我们每天工作的站点每天都有成千上万的点击量,所以使用JIT在最初的bump之后没有性能损失。当涉及到更小的网站开发时,看起来越少越好。

#2


21  

This is probably because an ASP.NET MVC project is a web application rather than a web site. In a web site, class files are compiled dynamically at run-time and must live in the App_Code folder. In a web application, everything is compiled statically and class files can live anywhere in your web application.

这可能是因为ASP。NET MVC项目是一个web应用程序,而不是一个web站点。在web站点中,类文件在运行时是动态编译的,并且必须位于App_Code文件夹中。在web应用程序中,所有内容都是静态编译的,类文件可以驻留在web应用程序的任何位置。

As David Brown pointed out, it's generally recommended to put extra class definition in a separate class library and then reference that from your web application. If you write unit tests or reference your classes from config files, it can be challenging or impossible to access these classes if they are defined only within your web application.

正如David Brown指出的,一般建议将额外的类定义放在一个单独的类库中,然后从web应用程序中引用它。如果您编写单元测试或从配置文件中引用类,那么如果这些类仅在web应用程序中定义,那么访问这些类可能是困难的,也可能是不可能的。

#3


10  

Generally, I place "service layer" and "utility" classes in a separate project and add it as a reference to my web application. With the MVC framework, I don't really see a need to compile classes at runtime.

通常,我将“服务层”和“实用程序”类放在单独的项目中,并将其添加为对web应用程序的引用。使用MVC框架,我不认为需要在运行时编译类。

#4


0  

For those who don’t want a second project/class library and just want a simple folder for a few c# classes, I propose “App_Classes”.

对于那些不想要第二个项目/类库而只想要几个c#类的简单文件夹的人,我建议使用“App_Classes”。