Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Have multiple ASP.NET web projects share pages and controls 3

Status
Not open for further replies.

MrCeri

Programmer
Jul 17, 2006
5
0
0
GB
Hi,

I have an existing ASP.NET/C# web application which I would like to replicate for use on another project. The other project will need slight modifications to the existing site, e.g. some different site text, some different logic in code-behind, the odd extra page here and there etc., but 95% of the site will remain the same.

My question is, what is the best practice for sharing code/pages between these two sites? I am using Visual Studio 2005, and cannot see an easy way of sharing .aspx pages, .ascx web controls, etc. between different web projects.

Naturally I don't want to simply create a copy of every page for each site, as this leads to twice the maintenance cost of having all the shared pages in the same place.

Many thanks in advance for any help/advice,

MrCeri
 
I have the same situation with a series of sites that I maintain. My first attempt was to create one "super-site" - that is, one VS web project used for all the sites. I had a "Site" table in the database that has SiteID, host-name, what master-page to use, etc, and the app would pick out the host name from the url and lookup the SiteID in the database to determine what masterpage to use, what data to retrieve, etc. This works ok up to a point, but it starts getting painful when there are slight deviations on logic, little changes in wording here and there.

So, I think you pretty much have to accept that there needs to be a distinct set of page files for each site. This way you can control each site's look, navigation, logic, etc, independently. What you don't want of course, is duplication of code when you don't need to. To me the most logical solution is to develop a framework. The framework should contain all the stuff that is common to all sites such as the core business logic, re-usable controls and the data-access code that goes with it. The trick is to make it generic so it can be powerful enough for all sites, but not so complicated to use that it becomes a pain in itself from excessive configuration and odd conventions.

So, start out with your usual web project in a VS solution. Then create the framework project by adding a class library project to the solution, and then have the web site reference it. To start bulding the framework, take a page that uses the code-behind model in the web project and take the code-behind class and move it to the framework assembly. Then change the aspx page back to in-line code model, and have it inherit from the code-behind class in the framework. Be sure to make all the events, class members and methods protected (not private) and overridable. This way, you can substitute alternate behavior in the pages' in-line code if you need to , or just let them use the default implementation from the original code-behind. Then you can do this will the ascx controls, too. What you end up with is a web site that consists almost entirely of just the html part of the aspx pages, and a dll that has all the code-behind. Sound familiar to VS 2003? The major difference is that the code-behind classes are in a different project and maintained completely independently from the aspx pages. This is what lets you use the framework in another website project.

Of course, this solution is not perfect but it's a start. It's likely that there is a more elegant way that I haven't thought of. I'd be interested in hearing how others that done this. If you have any more questions about implementing what I have described, please ask.



 
Thanks Dragonwell, that's a great response.

I agree, trying to store everything in one "super-site" and switching based upon what site is being accessed at any given time can only lead to headaches down the road.

I like your framework idea - naturally it's easy to share logic and data code between sites, as they are most likely already compiled into separate, easily referenced assemblies (they are in my case).

Similarly database scripts - have one master database script, then a "patch script" for each site, which adds/modifies/deletes any extra/unwanted fields and tables.

The pages are the tricky bit - though not perfect, I think you present a pretty workable solution there by moving as much of the logic as possible into a shared assembly. It's a shame it moves away from the .NET 2.0 way of doing things, and back to a more 1.1 model, but it's certainly better than anything I've come up with, so please don't take that as a criticism!

Incidentally, another suggestion I have seen posted elsewhere is to use your source control application to share the pages for you. I think this could be a pretty good solution too, I'll try and give both techniques a run through their paces tomorrow, and post back here if I learn anything interesting.

If anyone else comes up with any other ideas in the meantime, please let me know.

Thanks again,

MrCeri
 
For anyone who is interested, here are the solutions I've had suggested to me so far:

1) Copy all of the pages to the new website, and maintain both sites independently.
- obviously terrible from a maintenance point of view.

2) Move all of the code behind into classes in a shared assembly, and have the aspx pages inherit from these classes. It will still be necessary to have copies of all pages on each site, but the pages would now be super-thin, almost just html. All the logic from the code behind would now be in classes that can be shared between sites.
- still not ideal, as you still have to have a copy of each page maintained on each site, but at least there's less maintenance.

3) Use source control to maintain a copy of each web page in each of the sites.
- I haven't investigated this in too much detail, but I'm pretty sure it's possible. The danger here is from accidents happening where half the files in a directory are symbolically linked to other files, half are locally modified versions, and it all gets a bit confusing.

4) Re-factor all of my pages as re-usable controls, and move them into a shared class library.
- I guess it's an extension of option 2, but taking it a big step further (and greatly reducing maintenance costs).

This article certainly helps if anyone is interested in the details:


Cheers,
MrCeri
 
According to the screenshot in the link you provided, you can go to New Project - Visual C# - ASP.NET Web Application and I don't see that anywhere--it seems like I can only create one from "New Web Site". Does anyone know of a workaround for this? Visual Studio 2005 is already giving me a headache. :/

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
I think this is referring to the web application project model that was not part of the default install in vs 2005. see
[blue]_______________________________________[/blue]
Business Logic:"AND when tweetle beetles battle with paddles in a puddle, they call it a tweetle beetle puddle paddle battle AND..." - Dr. Suess
 
BlueRose156 - web applications are not supported as standard by Visual Studio 2005, though they will feature in future versions of VS.

For now you need to download and install the "Visual Studio 2005 Web Application Project" update - follow the link I gave previously, and click on "Home" in the menu bar. Alternatively, go here:


Cheers,
Ceri
 
Sorry for repeating what Dragonwell said, we must have posted at the same time
happy.gif


Ceri Williams,
 
well, you said it better :-D

[blue]_______________________________________[/blue]
Business Logic:"AND when tweetle beetles battle with paddles in a puddle, they call it a tweetle beetle puddle paddle battle AND..." - Dr. Suess
 
Oh I don't care who posted first--you both get stars from saving me from several more grey hairs. Thank you!!

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top