.NET (Visual Studio) Share assets between projects - c#

I'm working with Visual Studio. There I have a solution with several web-projects (.net MVC 4). All of these web-projects use the same javascript-libs. Currently I copied the library into each project, but this can't be the final solution. What is the best approach to share those library (assets in general) between all of the projects? Just link them? Or is it possible to create a project and reference it in all projects?
Update
"Link" the javascript files from another project is not a possible solution as I would have to link thousands of files (one of the libraries I am using is ExtJs) what makes it impossible to build a project without freezing visual studio...
Possible solution
Currently I have a (Web) MVC Project called "Web" and a (Class Library) Project called "ClientScript" which contains all the JavaScript files which are shared between several Web Projects. As linking all the needed JavaScript files is not a possible solution (because it's a matter of thousands of files what causes visual studio to freeze) I copy all the needed JavaScript files to the individual Projects using the Build Events in each Web Project (Project -> Properties -> Build Events -> Post-build).
My Post-build command line in the Web Project looks like this:
start xcopy "$(SolutionDir)ClientScript\Frontend\*" "$(SolutionDir)Web\Scripts" /r /s /i /y /D /E
Every time you build your Web Project all the changed Javascript files get copied from the ClientScript Project to your Web Project.
While developing the Javascripts I run a small "filewatcher" tool which automatically copies a file from the ClientScript Project to every Web Project when it changes. This way I don't have to build the Web Project every time when I make a change to one of the Javascripts.

Anyone that stumbles across this question here in the future should know that there are now Shared Projects in Visual Studio to solve this problem. Universal Windows projects use them by default and you can create your own by downloading and installing the VS extension here: https://visualstudiogallery.msdn.microsoft.com/315c13a7-2787-4f57-bdf7-adae6ed54450
Note: At this time they can be picky about what type of project you try to add the shared reference. I created a JavaScript shared project to share js files between a Windows store js app and an MVC web app and it would not let me do that saying they had to be of the same language. (It supports C#, C++, or JavaScript).

Place the JS files in a single folder, likely above all others, and add them to the project but use the "Link" option. It's part of the drop down on the "OK" button in the "Add existing item..." dialog.

When you run every new ASP.NET MVC 4 project it's take a new port then other app have take.
I simply suggest you a simple thing.
run a project which contain all the pacakages. open them webmatrix and run them as localhost:80.
You need to set the port in settings section of your site in webmatrix. Now it will rechable at localhost now you can reference all the libraries from this packages.

Slightly older thread, but I have another way of doing a similar thing using Web Essentials, that handles the issue of not publishing correctly.
I have a shared folder outside of the projects that require the shared file, normally a 'common' project with other things in as well, but can be just a simple folder as suggested by Michael Perrenoud.
However instead of 'Add as Link' I have been creating a new bundle in the project that requires the shared js/css file, with the same name as the shared file, and then referencing that file in the shared folder using a relative reference location rather than the root based one it starts with.
To add a file from a shared folder in the root of the solution to the scripts folder use the following code in a new bundle file (*.bundle), changing the folder/file names as required.
<?xml version="1.0" encoding="utf-8"?>
<bundle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://vswebessentials.com/schemas/v1/bundle.xsd">
<settings>
<minify>false</minify>
<runOnBuild>true</runOnBuild>
</settings>
<files>
<file>../../MySharedFolder/my-js-file.js</file>
</files>
</bundle>
Then every time you build it recreates the bundle with the latest version, this version is then also published as expected :)
You can even create a minified version if desired by changing 'minify' to true. Or better yet you can add them loads as a bundle too if you want, you have that flexibilty.

This is an older thread but due to complex business requirements these days applications are divided in to different modules or sub projects.Thus, brings us the need to share common resources like JavaScript files, themes and CSS style sheet files.
I personally feel that common files should be put in separate Asp .Net MVC 5 project which has following structure :ASP.NET MVC5 folder structure
Now the best part is you can separately manage the dependencies using Bower,NPM or Nuget package manager.
After you have organised all the files in this project host this project to your own CDN or may be on cloud. You can use Using CDN in Bundle Approach to get script or link references.
That will help you sharing common resources across all the projects.There us a short coming though if you have many developers on the team and if someone added incompatible version lib can affect all the apps.

Related

Web project migrated from VS2013 to VS2015 cannot be published

I had an Asp.Net MVC project, in VS 2013 update 4, and now I continue working on it in VS 2015. The Asp.Net version is not upgraded, i.e. I still use the stable asp.net 4 and MVC 5. The problem is: I cannot publish this project anymore. During the publish, it complains about xml files of every library I use, like EF, OWIN, ... both for Nuget libraries and normal .net framework libraries.
I tried adding these xml files from other sources and the publish proceeds further, but it raises another error like this:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.targets(2991,5): Error : Copying file obj\Release\Package\PackageTmp\Content\bootstrap-rtl.min.css to obj\Release\Package\PackageTmp\obj\Release\Package\PackageTmp\Content\bootstrap-rtl.min.css failed. Could not find file 'obj\Release\Package\PackageTmp\Content\bootstrap-rtl.min.css'.
I removed this file from the project and the error still refers to this file. I searched the solution folder for this name, both in file names and file contents and I don't have it anywhere. It seems that Visual Studio has cached the publish procedure somewhere and does not update it when the project changes.
Any hints or solutions?
In our case the solution was to create a new clean project, then copy all source code files (C# code, javascript, css, razor templates, html, ..., but not for example, contents of bin and obj folders) from the old project into the new one, and then adding them to project (in solution explorer).
Don't waste too much time on solving this problem, dodge it.

Static Files in Class Library; How to Copy to Website Folder

In my Visual Studio Solution, I have a Website project, as well as a Web project (the latter a Class Library containing things like web-specific logic, HTTP handlers, etc.)
I would like to include shared static assets in the Web project like JavaScript and HTML template files. These files will be referenced and used in the Website but may be used in one or more other projects in the future, so I would like to keep them in a common project. Ideally they'll end up somewhere like /js or /templates in the Website. I'd prefer them not going in the /bin directory if possible, but I could be convinced that it's fine.
That said, what would be the most appropriate way to keep them in the Web project, but have them deployed with the Website at build/run and deployment time, whether debugging from Visual Studio or building with Jenkins/MSBuild? The options I see are "Copy to Output Directory" or some sort of embedded build action (not ideal, as I would like to copy the files into the output directory in their native format.)

Clickonce and postbackevent

I have a WPF project and a post build event which copy files from a folder outside of the solution into the output directory.
I want to publish my application with Clickonce publish. The only problem is that the copied files are not included in the publish or the manifest.
I tried using MageUI.exe , msbuild /target:publish from the visual studio cmd and even tried to change the project file by hand including a beforepublishevent but none of that worked.
I am open to suggestions, but what I want is to take the output folder and make the installer install the output files. (e.g. install the .net 4 framework and visual c++ runtime libs)
If you don't want them in the solution, but you do want them in ClickOnce then the only possibility is to use an external tool to create the ClickOnce manifests as you can't add non-project files to the ClickOnce output in Visual Studio.
There are two options I know of:
Use MageUI to manage your ClickOnce manifests. You can add additional files to your application in Mage.
You could use some software that I created and sell called ClickOnceMore (www.clickoncemore.net). ClickOnceMore will allow you to add all the files in one directory to your ClickOnce manifests by adding a single folder include. You can also then control which sub folder on the client they get deployed to. It was designed to make scenarios like this simple. You can download a free trial on the web site.
I hope it helps. Apologies for the marketing plug, but I do think ClickOnceMore can solve your problem perfectly.
Instead of copying the files through a post-build event, why don't you include them inside the project, and mark Copy to Output Directory = Copy if Newer?
If they are DLLs, you can add them as reference instead.
There are other options to create installers such as WiX+SharpSetup. It's much more flexible, but also more complicated, and takes more time to create simple installers.

For a Silverlight 4 Visual Studio solution, what needs to be in version control?

I have a Silverlight 4 app that I'm building with Visual Studio 2010. I'm using Mercurial/TortoiseHG to do version control. Which files do I need to check in? By default, it checks in all sorts of .dlls in /bin/debug and stuff. Do I really need those? Or can I just grab code and content files? Do I need to version something to keep track of project properties and references, or is that contained within the .csproj file itself?
You don't need to include stuff in /bin or /obj. This is true of all VS solutions in source control. These are recreated upon every rebuild. Also, for Silverlight specifically, you don't need to check in the XAP file that is generated in the ClientBin of your web app.
From MSDN (via this social.msdn thread):
You can add the following files to Visual Studio source control:
Solution files (*.sln).
Project files, for example, *.csproj, *.vbproj files.
Application configuration files, based on XML, used to control run-time behavior of a Visual Studio project.
Files that you cannot add to source control include the following:
Solution user option files (*.suo).
Project user option files, for example, *.csproj.user, *.vbproj.user files.
Web information files, for example, *.csproj.webinfo, *.vbproj.webinfo, that control the virtual root location of a Web project.
Build output files, for example, *.dll and *.exe files.
It doesn't say anything specific about Silverlight projects though.
Is Mercurial/TortoiseHG integrated into Visual Studio? i.e. can you check out/submit from within VS?
If so, if you right click on the project name and select "Add Solution to Source Control" it should add those parts of the project that it needs ignoring everything else.

SharedAssemblyInfo.cs

I am using a SharedAssemblyInfo file which seems to be a 'standard' technique:
http://blogs.msdn.com/jjameson/archive/2009/04/03/shared-assembly-info-in-visual-studio-projects.aspx
I put the SharedAssemblyInfo.cs file into my web application because with web site projects I cannnot 'add files as a link'.
But now I need to add a second web project to the solution and obviously the SharedAssemblyInfo file cannot be within both projects.
How do I get round this problem without having duplicate SharedAssemblyInfo files?
One way to solve this problem from within a web site as opposed to a web project is via your source control provider.
If you are using Visual Source Safe you can "share" the file to both projects (folders) and not branch. If you are using subversion 1.6 you can use a single file "externals" property. See this page for info.
In the case of Team Foundation Server, you can look at this codeplex article. I don't have any experience with TFS, but I think in the case of a SharedAssembly.cs the branch and merge solution would work fine.
This means you will have to check the file in in one location and check it out in the other location for the change to take effect. However, have the AssemblyInfo out of sync during the development process probably won't break anything.
Is it a web site or a a web project? With a web project the linked file should work fine... although personally I'd rather keep them separate and update them through my build script.

Categories