12
votes

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.

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.

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

7 Answers

8
votes

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.

7
votes

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.

Optionally, you can create an assembly project and put your classes in there and just reference it from your WAP.

Ultimately the structure is going to boil down to how many classes you will have.

2
votes

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.

I would highly recommend that you consider this approach.

1
votes

I normally have three projects within a solution. The web app, the web library (base pages etc) and the DAL. This keeps everything clean.

1
votes

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.

1
votes

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

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

0
votes

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.