c# asp.net Centralized UI Development - asp.net

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.

Related

Custom path for folder

How to add a reference to the App_code directory for a sub-folder in web project
I have been given a task to take an older, grown piecemeal over a decade internal website and bring it "up to speed". I have run into a problem in ASP files to "see" the app_code directory. Essentially the current site is a collection of folders that work(ish) that I am trying to collect together as a single project with the LEAST number of edits.
When functionality was superseded the old code was moved into a "deprecated" folder. The code files now reference object in the app_code directory which is not visible to the file from it's new location. The easy answer would be to copy a version of the class file into the "deprecated" folder but that seems more like a hack then a solution. The same for copying the App_code folder to a location visible to the ASP file.
My thought was to add a reference ("reference path") somehow to the deprecated folder pointing to the app_code folder but I cannot find how to do that.
A couple of other notes: 1) the site was built by using text editors and "freebie" editors - it has never been compiled or run in-total through VS before. 2) once I get it built and checked into TFS I am going to remove the deprecated folders. 3) I am working under some stipulations to satisfy concerns arising from actions of prior developers.
Code like:
tr.Controls.Add(GUI.GetTableCell("Salesman", 0, "Center"));
Works well when the ASP page is at the same directory level as the App_code folder ("GUI" is a class with a "GetTableCell" function).
If I can add a folder reference I can solve all of the remaining problems I have with this step in the project.
EDIT
Let me re-ask it this way. The image below is a composite of what I am facing in VS2017. I have such a feeling that a light bulb is simply not turning on for some reason. How I have done this so far is pulling down the current website from our internal Win2003 web server and am trying to convert it into a legit web project. I have done many of the steps necessary but there are 47 instances of the problem visible in the composite image from Visual studio
The code behind from SubscriptionEditor.aspx pictured lower right. Has reference to a namespace which is in the code files in the App_code folder (neatly hidden). This site functions fine but I am missing something to make it work from within VS. I am just at a loss.
For anyone that finds this I want to point out that there are a number of excellent answers available for similar questions here and i have looked at dozens and tried them all. The simple answer to my problem is "you can't".
How to convert ASP.NET website to ASP.NET web application came close
The problem being the source is just a collection of ASP files - it follows no project or template.
Namespaces are super important in development and the clever ways that prior developers found to breach that discipline can not be fixed through VS. It is simple a lot of bloody knuckle corrections of code and scope issues til the errors disappear.
The limitations put on me prohibit any solution. I will end up advocating not "saving the code" unless they are willing to put in the investment to heal the most egregious foibles. So if you see me in the "available for contract" sites then you know how that conversation went.

Splitting ASP.NET application in two applications - handling of shared pages / user controls / scripts

I have a big ASP.NET application (legacy) which actually (functionally) is composed from two portals. So I need to split it to two separate applications, to ease the development on each of them.
Of course there are shared features between the two. Some of them are in DAL and BL, and that is not an issue - all that code was separate din separate projects, which made up assemblies that are to be referenced in both apps.
But the problem is with some pages, lot of user controls, some css and javascript files, which are shared between the two "portals" (applications).
I'd like to ask for some advice on how to handle them. My main concern is to avoid duplication, so ideally they should stay in a single place, and be used by both apps.
First I tried was to add files from one project to the other as linked files. While this works for code file (they get built into the project they are linked to), it doesn't for aspx / ascx or css / javascript / images. It does if I publish first (if marked as content, they get copied during publish), but I can't do this all the time during development, and such files are not found when app is debugged / run from source code (sincve, obvious the linked files are not actually available in app file tree, when one is looking for any of them.
Another thought was to create pre-build event, and in that to copy all shared files from a common location.
e.g. I create a project Common and put there all files that are shared between applications, organized on folders, and on pre-build I perform an xcopy.
And another thoughts is to make all shared files part of a SVN repository which I reference with svn:external, in both projects.
But all looks to my little cumbersome. Does anyone had similar situation? How did you handled it?
Any advice on any of my suggestions?
You have, at least, two options :
sharing through virtual directories : https://stackoverflow.com/a/13724316/1236044
create user control libraries : https://stackoverflow.com/a/640526/1236044
The virtual directories approach seems straightforward for ressources like css, js, images.
I also tend to like it for sharing user controls.
The library approach should need more work, but would ensure better reusability of the controls on the long run.
I had an identical problem this week with css and javascript files triplicated across three legacy projects.
I removed the files from two of the projects and replaced them with linked files to the first project, but when I ran the website I got 404 errors for css & javascript files missing in the pages belonging to the two projects.
So I simply added the nuget package 'MSBuild.WebApplication.CopyContentLinkedFiles' to my solution and everything worked fine - the css and javascript files were deployed fine for the two projects and my 404 errors disappeared.
I didn't have any shared .aspx / .ascx files, but I would imagine it will work for them too.
See also this question / answer.

Where to save classes of the web-application

I have to create a small asp.net-application. The last such web-application I have built was a while ago. At this time, I have saved my classes in the App_Code-folder.
During setting up the web-solution in VS2010, I have seen that VS does no more propose to create the app_code directory (right click on the project, Add asp.net-folder). Is the App_Code folder no more the prefered location to save classes of the web-application and where is the new place to store them?
Update
Thanks to Oded I have received the answer to my question.
If one has the same question as I had, probably he is also not aware (as I was), that there are two different types of projects with their own menu-points in VS: Web-Application and Web-Site. The organisation of these two types is different. For a web-application, the app_code-folder is not proposed in the asp.net-folder-menu . Maybe this information helps someone.
It is the preferred place, but VS 2010 just doesn't automatically create it for you any more.
As you can see from the documentation of app_code for .NET 4.0, it is still the best place for shared code.
In a Web site project, you can store source code in the App_Code folder, and it will be automatically compiled at run time. The resulting assembly is accessible to any other code in the Web application.

Using the same App_Code classes across websites

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

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