C# 当使用 Web 应用程序项目类型的 Visual Studio .NET 而不是网站时,我应该把类放在哪里?(ASP.NET)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/894052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where do I put classes when using Web Application project type of Visual Studio .NET instead of Website? (ASP.NET)
提问by Jader Dias
I have plenty experience creating ASP.NET Websites in the Visual Studio. But there is an alternative way to do the same thing that is through Web Applications, which have slightly different file structure.
我有很多在 Visual Studio 中创建 ASP.NET 网站的经验。但是还有一种替代方法可以通过 Web 应用程序来做同样的事情,它们的文件结构略有不同。
Since I created my first Web Application I couldn't use classes (.cs files) in the App_Code folder anymore, they were not seen by the ASPX and ASHX classes unless were moved to the same file.
由于我创建了我的第一个 Web 应用程序,我不能再使用 App_Code 文件夹中的类(.cs 文件),除非将它们移到同一个文件中,否则它们不会被 ASPX 和 ASHX 类看到。
It happens that I use the same classes across many files and I don't want to have multiple copies of them. Where do I put those classes? There is any solution without creating another project?
碰巧我在许多文件中使用相同的类,我不想拥有它们的多个副本。我把这些课放在哪里?有没有不创建另一个项目的解决方案?
采纳答案by Vikram
We have been using Web Application project type in VS 2008 for all our projects and put our common classes in AppCode folder instead of App_Code folder. It works absolutely fine, we access our classes across all the pages in the application without any problem at all.
我们一直在 VS 2008 中为我们所有的项目使用 Web 应用程序项目类型,并将我们的公共类放在 AppCode 文件夹而不是 App_Code 文件夹中。它工作得非常好,我们可以在应用程序的所有页面上访问我们的类,没有任何问题。
回答by Andrew Hare
Why do you not want to create another project? This would be the simplest approach as all your classes would be housed in that assembly which you could project-reference in your web application and then have access to everything across the entire project.
你为什么不想创建另一个项目?这将是最简单的方法,因为您的所有类都将位于该程序集中,您可以在 Web 应用程序中对其进行项目引用,然后可以访问整个项目中的所有内容。
I would highly recommend that you consider this approach.
我强烈建议您考虑这种方法。
回答by NotMe
With Web Application Projects you have a lot more freedom. Just create subfolders under your project to hold your classes. For example, you could have a folder named "DAL" to hold the Data Access Layer items.
使用 Web 应用程序项目,您有更多的自由。只需在您的项目下创建子文件夹来保存您的课程。例如,您可以有一个名为“DAL”的文件夹来保存数据访问层项目。
Optionally, you can create an assembly project and put your classes in there and just reference it from your WAP.
或者,您可以创建一个程序集项目并将您的类放在那里,然后从您的 WAP 中引用它。
Ultimately the structure is going to boil down to how many classes you will have.
最终,结构将归结为您将拥有多少个类。
回答by azamsharp
I highly recommend that you put all your classes (domain objects) in a separate project. This way you will be easily able to write test against your business layer (domain objects) and your classes will be portable. Portable means that you can send your DLL to another developer and he/she can easily reuse the classes you developed.
我强烈建议您将所有类(域对象)放在一个单独的项目中。通过这种方式,您将能够轻松地针对您的业务层(域对象)编写测试,并且您的类将是可移植的。可移植意味着您可以将您的 DLL 发送给另一个开发人员,他/她可以轻松地重用您开发的类。
回答by George
I normally have three projects within a solution. The web app, the web library (base pages etc) and the DAL. This keeps everything clean.
我通常在一个解决方案中有三个项目。Web 应用程序、Web 库(基本页面等)和 DAL。这使一切保持清洁。
回答by Jason Kester
Put them anywhere you want. I tend to keep the little project-specific helper classes and base pages in a /Helpers folder under the web project, but split out DataLayer stuff and general-purpose reusable helpers to their own separate projects.
把它们放在你想要的任何地方。我倾向于将特定于项目的小助手类和基页保留在 web 项目下的 /Helpers 文件夹中,但将 DataLayer 内容和通用可重用助手分开到他们自己的单独项目中。
回答by Theophilus
I use /Shared/Classes
for general purpose classes used throughout the site. I like putting the rest of the classes in a Classesfolder where they are used such as /blog/Classes/
.
我/Shared/Classes
用于整个站点使用的通用类。我喜欢将其余的类放在Classes文件夹中,在那里使用它们,例如/blog/Classes/
.
-- EDIT--
- 编辑 -
The answer above was how I stored classes in Web Forms application projects. Now that I am using MVC, I store general purpose classes in /Classes
and non-general classes in subfolders under /Classes
such as /Classes/Blog
. In short, Old_App_Code
has been renamed to Classes
. This seems like a natural extension to the naming conventions I see Microsoft using in MVC, plus it works with my old Web Forms pages too.
上面的答案是我如何在 Web 窗体应用程序项目中存储类。现在我正在使用 MVC,我将通用类/Classes
和非通用类存储在子文件夹中,/Classes
例如/Classes/Blog
. 总之,Old_App_Code
已经改名为Classes
. 这似乎是我看到 Microsoft 在 MVC 中使用的命名约定的自然扩展,而且它也适用于我的旧 Web 窗体页面。