在使用Visual Studio .NET的Web应用程序项目类型而不是网站时,我在哪里放置类? (ASP.NET)

时间:2021-08-07 03:33:43

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应用程序执行相同的操作,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?

碰巧我在许多文件中使用相同的类,我不想拥有它们的多个副本。我在哪里上课?有没有创建另一个项目的解决方案?

7 个解决方案

#1


7  

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文件夹中。它工作得很好,我们在应用程序的所有页面*问我们的类,没有任何问题。

#2


6  

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.

最终,结构将归结为您将拥有多少课程。

#3


2  

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.

我强烈建议您考虑这种方法。

#4


1  

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。这可以保持一切清洁。

#5


1  

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的东西和通用的可重用帮助程序拆分到它们自己的单独项目中。

#6


1  

I use /Shared/Classes for general purpose classes used throughout the site. I like putting the rest of the classes in a Classes folder 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。这似乎是我看到微软在MVC中使用的命名约定的自然扩展,而且它也适用于我的旧Web窗体页面。

#7


0  

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发送给其他开发人员,他/她可以轻松地重用您开发的类。

#1


7  

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文件夹中。它工作得很好,我们在应用程序的所有页面*问我们的类,没有任何问题。

#2


6  

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.

最终,结构将归结为您将拥有多少课程。

#3


2  

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.

我强烈建议您考虑这种方法。

#4


1  

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。这可以保持一切清洁。

#5


1  

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的东西和通用的可重用帮助程序拆分到它们自己的单独项目中。

#6


1  

I use /Shared/Classes for general purpose classes used throughout the site. I like putting the rest of the classes in a Classes folder 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。这似乎是我看到微软在MVC中使用的命名约定的自然扩展,而且它也适用于我的旧Web窗体页面。

#7


0  

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发送给其他开发人员,他/她可以轻松地重用您开发的类。