简体   繁体   English

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

[英]Where do I put classes when using Web Application project type of Visual Studio .NET instead of Website? (ASP.NET)

I have plenty experience creating ASP.NET Websites in the Visual Studio. 我有很多在Visual Studio中创建ASP.NET网站的经验。 But there is an alternative way to do the same thing that is through Web Applications, which have slightly different file structure. 但是有一种替代方法可以通过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? 有没有创建另一个项目的解决方案?

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. 我们一直在VS 2008中为所有项目使用Web应用程序项目类型,并将我们的公共类放在AppCode文件夹而不是App_Code文件夹中。 It works absolutely fine, we access our classes across all the pages in the application without any problem at all. 它工作得很好,我们在应用程序的所有页面上访问我们的类,没有任何问题。

With Web Application Projects you have a lot more freedom. 使用Web应用程序项目,您可以获得更多自由。 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. 例如,您可以使用名为“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. 最终,结构将归结为您将拥有多少课程。

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. 我强烈建议您考虑这种方法。

I use /Shared/Classes for general purpose classes used throughout the site. 我将/Shared/Classes用于整个站点中使用的通用类。 I like putting the rest of the classes in a Classes folder where they are used such as /blog/Classes/ . 我喜欢将其余的类放在Classes文件夹中,使用它们,例如/blog/Classes/

-- EDIT-- - 编辑 -

The answer above was how I stored classes in Web Forms application projects. 上面的答案是我如何在Web窗体应用程序项目中存储类。 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 . 现在,我使用MVC,我存储在通用类/Classes下的子文件夹和非通用类/Classes/Classes/Blog In short, Old_App_Code has been renamed to Classes . 简而言之, Old_App_Code已重命名为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. 这似乎是我看到微软在MVC中使用的命名约定的自然扩展,而且它也适用于我的旧Web窗体页面。

I normally have three projects within a solution. 我通常在解决方案中有三个项目。 The web app, the web library (base pages etc) and the DAL. Web应用程序,Web库(基页等)和DAL。 This keeps everything clean. 这可以保持一切清洁。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从ASP.NET 4网站项目迁移到Visual Studio 2010中的Web应用程序项目是否导致所有控件引发错误? - Migrating from ASP.NET 4 website project to web application project in Visual Studio 2010 Causing all controls to throw error? 在构建ASP.NET网站时,如何查看Visual Studio生成的元项目文件? - How do I view the metaproj files generated by Visual Studio when building an ASP.NET website? 如何解决在加载ASP.NET MVC项目时挂起的Visual Studio? - How do I troubleshoot Visual Studio which is hanging when loading ASP.NET MVC project? Visual Studio中使用ASP.NET Web API项目的Angular项目 - Angular project with ASP.NET Web API project in Visual Studio 如何在Visual Studio 2022中打开asp.net网站项目? - How to open asp.net website project in Visual studio 2022? Azure DevOps与asp.net网站项目而不是Web应用程序的持续集成 - Azure DevOps Continuous Integration with asp.net website project instead of web application 如何使用.net 3.5在Visual Studio 2008,asp.net Web应用程序中生成QR码? - How to generate QR code in visual studio 2008,asp.net web application using .net 3.5? 运行Asp.Net网站项目时如何在Visual Studio 2010中禁用脚本调试? - How to disable script debugging in Visual Studio 2010 when running Asp.Net website project? ASP.NET web 应用程序无法在 Visual Studio 2019 中发布 - ASP.NET web application fail to publish in Visual Studio 2019 Visual Studio 2017 中的空 ASP .NET Core Web 应用程序项目 - Empty ASP .NET Core Web Application project in Visual studio 2017
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM