Using the same App_Code classes across websites - asp.net

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.

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.

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.

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

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

Related

You have two ASP.NET projects. One contains a copy of the other's dll, and a reference to it. Can you run the projects on two different servers?

My question is about the necessary proximity of a dll to the project from which it was created. If you have two ASP.NET projects, and you copy the dll created by one of them, into the other project, (for example, putting it in a library folder), and then add a reference to that copy so it can be used, do the two projects need to run on the same server?
Many thanks!
Yes. You can definitely do this. You have to ensure that the referenced dll files are getting copied over. And as they are separate applications, you can easily deploy them on separate servers. Though I am not entirely sure why do you need to add a reference of ASP.NET project to another ASP.NET project. You might be able to achieve sharing of code by creating separate class library project and referring that.

c# asp.net Centralized UI Development

We have a lot of websites with common functionality developed by 3 persons, in the business logic we use a common library project (in a shared directory) so we all use the same functions. This way the corrections and improvements are shared for the following projects or when we recompile an existing project. We have a class for some UI common functions too (loading a ListControl with x data and so)
The problem is with some web parts like CSS, Javascripts, Common Pages (login, configuration, customer management), those we don't know exactly how we can centralize them so we have those parts in the shared project so we don't have to copy paste corrections/improvements manually to the other websites each time...
Example of current website structure:
-MyWebSite1
-Styles.css
-Scripts.js
-Login.aspx
-Funx.cs (Functions specific to this site)
-Consx.cs (Session and other variables specific to this site)
-CommonProject (In a network shared directory)
-FunBusiness.cs
-FunWebUI.cs
-ConsBusiness.cs
-ConsWEB.cs
Is there a way of doing this?
For now the closest we have come to solving this problem is following this article for the Javascript part:
http://msdn.microsoft.com/en-us/library/bb398930(v=vs.100).aspx
We are now investigating using only one reference to a js file and including the other javascript references dinamically and the common CSS and MasterPages parts...
Maybe you can add those common references files as Linked File in Visual Studio. In this way you can maintain one file, while kept in a different location.
From Microsoft:
Link file leaves the file in its current location and maintains a link to the file from your current project.
Another solution would be to create a copy script before compile in Visual Studio. Reference over here.

Registration-Free COM in ASP WebPage

I have a Webpage application which uses in the code behind DLL's and OCX's, some of these DLLs are VB6 ones and the others are C++. At the moment the access to these DLL's / OCX's is through the registy, i would like to change this using RegFree COM.
Problem: All examples i have found until now are demonstrating how to modify an EXE which accesses DLL to Registry Free one using the manifest files, assuming we have one EXE and one DLL the Registration Free access would produce one manifest file for the EXE (in which the dependency to the DLL's mnaifest is set) and another one for the DLL (which references the DLL), in my case i dont have an EXE but a Browser, so i am stuck with the EXE's manifest.
Is it possible to use Registry Free COM in my scenario? if yes where do i set the dependency between EXE->DLL, is it in somewhere Visual Studio??
thank you.
Perhaps you might look at http://www.mazecomputer.com/sxs/help/iis6config.htm which describes the process required.
If you truly mean ASP.Net try http://www.mazecomputer.com/sxs/help/iis6aspnet2.htm

Moving Functionality to a class library project - what to do with the related resources?

I have a .net project whose part of the functionality is xls transformation.
I now want to move that functionality away from the main project to a separate class library project.
I have no problems moving classes and accessing them, but part of the resources that I wanted to move were xslt files that define the transformations. Those files were originally located in the special asp.net folder: App_Data and then accessed using Server.MapPath(~/App_Data/XSLT/anXsltFile.xslt)
I wanted to move those files to that separate library as well, but not sure how to approach it, and how to access those files within the class library.
Perhaps embed the xslt files inside your class library and stream read them as necessary to perform your transforms
http://support.microsoft.com/kb/319292
How to embed a text file in a .NET assembly?
http://blogs.msdn.com/b/alexdan/archive/2007/12/19/loading-embedded-resources-in-c-using-getmanifestresourcestream.aspx
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/1d341eae-fed4-408c-8791-96e96a5fd99c/
I think a class library, unlike a web site project, should not have file resources. It should supply the functionality to the web site project which will host the files.
I personally consider both the class library and the xslt files to be resources of the web site. Lets say you want to use this same class in a different app or web site, with different xslt files.
It's bad for re-usability.
I suggest you to use string constant in Web.Config with the Server.MapPath path and use these to load the xslt files.
I hope it's helpful
if you mantain the xsltfiles on the app_data, you must use
httpcontext.current.server.mappath("...your path...")
if you move the xslt files to the assembly too, you must get the Assembly resource...

Separating Web Applications into multiple projects

I have a web application that is becoming rather large. I want to separate it into smaller more logical projects, but the smaller projects are still going to need to access some of the classes in the app_code of the main project. What are some good methods to accomplish this?
Add a class library project with the common classes and add a reference to this project to each of the new projects.
So you'll have the following Solution layout
/webapp1
/default.aspx
/....
/webapp2
/default.aspx
/....
/lib
/Utils.cs
If you are only looking for a way to organize your files, then you can create a folder for each sub-project. This way you'll be able to get to the content of app_code and maintain a level of separation with very little rework.
If you are looking for the best way to do this, then refactoring your code to have a common Class Library based on what is reusable in the app_code folder and multiple, separate projects that reference that library is the way to go.
You may run into problem refactoring the code this way, including not being able to reference profile or user information directly. You are now going from the Web Site to Web Application paradigm.
http://www.codersbarn.com/post/2008/06/ASPNET-Web-Site-versus-Web-Application-Project.aspx
Extract your common code from app_code into a class library which is referenced by each of your other projects.
I like the 3 Tier approach of creating a data access project, a separate business project, then use your existing site code as the presentation layer, all within the same solution file.
You do this, like posters before me said, by creating Class Library projects within your existing solution and moving your App_Code classes to the appropriate layer and then referencing the data access project in the business project, and the business project in the web project.
It will take a bit of time to move it all around and get the bits and pieces reconnected once you move so make sure you set aside plenty of time for testing and refactoring.
In CVS & Subversion, you can setup what I think are referred to as "aliases" (or maybe it's "modules"). Anyway, you can use them to checkout part(s) of your source control tree. For example, you could create an alias called "views" that checks out all your HTML, javascript, and css, but none of your php/java/.NET.
Here's an example of what I'm doing within my projects.
The basic idea is to have all common files separately from htdocs so they are not accessible by client directly and sharable.
Directory structure:
public_html
The only htdocs dir for all projects.
Stores only files which should be directly accessible by client, ie js, css, images, index script
core
Core classes/functions required by application and other scripts. Framework in other words.
application
Stores files used to generate separate pages requested by public_html/index script + classes common to all projects
config
Configuration for all projects, separated by project
templates
Template files separated from all other files
The public_html/index script is then used for all projects on all domains/subdomains and based on the requested URL loads proper pages...
A somewhat simple approach is to group the code in your app_code folder into it's own assembly. The only issue that you could possibly run into is if the code in your app_code folder is not decoupled from the elements on you pages (This is normally always a bad idea since it indicates poor cohesion in you classes).
Once you have your code in a separate assembly you can deploy it to any number of servers when you are upgrading you apps.

Resources