Freigeben über


Referencing a class which is outside of App_Code folder in an ASP.Net Website project

Came across with this situation yesterday. The solution turns out to be simpler than expected. All we have to do is to get the class compiled in a dll and reference it!

 

Here are the steps

 

1. Open VS 2005/2008 –> Create New WebSite

2. In the Website project, Create a new folder (MyFolder)

3. Add a class file in it (Class1.cs) and put whatever code in it

4. Now open Visual Studio Command Prompt, goto the project folder (C:\Documents and Settings\..\WebSites\WebSite1\MyFolder)

5. Compile the class in a dll (>csc /target:library Class1.cs). It will create Class1.dll and will put it in MyFolder

6. Now reference this in your project (Add Reference)

7. Now Class1 is ready to be used within the application

 

Depends on the requirement, you may also want to consider

1. Using a Class Library project and referencing it in the WebSite project

2. Or using Web Application Project instead of WebSite

 

Hope this will help!

Ashish