views:

665

answers:

4

Let's say you have a solution with two website projects, Website A and Website B. Now inside Website A's App_Code folder, there is a Class X defined in a ClassX.cs file. What do you do if Website B also needs access to ClassX.cs?

Is there any way to share this file across App_Code folders? Assume that moving the file to a common library is out of the question.

+1  A: 

Please please don't use these unholy website projects. Use Web Application projects instead, pack your shared classes into a library project and reference it from all your Web Applications.

JRoppert
'Unholy' is a rather strong word. There are legitimate reasons to use the WebSite model. Not every web application is a three-environment managed-deployed enterprise application.
Euro Micelli
I don't see any reason to use website projects. I think web apps are more professional regarding the use of a development process, build process, QA, shared development and so on. What are your reasons for using website projects? And yes, 'unholy' might be a bit to strong - but only a bit ...
JRoppert
+1  A: 

Pack your shared classes into a Library (a DLL) and from each site right-click on add reference and select the library that you have created.

Jon Erickson
A: 

I don't believe that there is a way without moving ClassX into a new code library project. .NET requires all an assembly's dependencies to exist in the same folder as the assembly itself, or in the GAC, to be automatically detected.

You could try loading the assembly manually via the Reflection classes, although it's a bit hacky.

The best solution, if you have the time available and the inclination to undertake it, would be to go with JRoppert's solution of moving it to a web application project. You could then use web references (which work about as nicely as regular references inside VS) to refer to ClassX.

HTH

alastairs
+1  A: 

With the restriction of "Assume that moving the file to a common library is out of the question." the only way you could do this is to use NTFS junction points to essentially create a symlink to have the same .cs file in both folders.

This is a terrible option though (for versioning reasons)...moving it to a common library is the best option.

Here's the Wikipedia entry on NTFS junction points http://en.wikipedia.org/wiki/NTFS_junction_point

and here's a tool for creating them http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx

James
I've been trying to get this to work for 2 years now (versioning is not an issue in the project I work on). I never knew about Junctions. Thank you so much!
Frank Edwards