I have multiple ASP.NET MVC5 projects that use several similar styles/scripts, and I think, for maintenance's sake, it'd be ideal to have a separate project in Visual Studio 2013 containing the LESS stylesheets and JavaScript files that can be shared between the two.
My setup is as follows:
Web.Project1
Web.Project2
Web.Assets //would contain LESS/JS files
Resources
Domain
Data
Any pointers?
I've been looking at adding as a link, but I'm not certain that would accomplish what I need.
I recently went through this process for a project of mine, and concluded that if your goal is simply to avoid duplication by keeping your code DRY then using linked files works great.
It's simple enough to do, but I'll write it up anyway as it might save someone some time.
Quick Summary
Create common Solution Folders containing the resources to be shared, or simply designate one of the projects to be the master.
Use "Add as Link" to add the shared resource files to each project as needed.
Add an AfterBuild task to each project file that will copy the linked files to project folders. This is only needed so that Visual Studio test/debug (F5) will work locally.
Detailed explanation follows:
Configuring Solution Folders for the Shared Resources
** Note that if you're simply going to share files directly from one project to one or more additional projects then you can skip this step.
Visual Studio solution folders do not have to reflect physical file system folders, but doing so will help preserve your sanity. So first create the folders on your local file system and copy the resource files into them. The new folders should be located under your solution folder. For example:
\MySolution
\Common
\Images
\Scripts
\Styles
Back in Visual Studio, right click on the Solution Items folder and use Add Solution Folder to replicate the new file system folders.
Next, add the files to the new solution folders by right-clicking each folder and using Add Existing Item to add the contents of the folders.
Add Shared Resources as Links
For each project that will use the shared resources, right-click the project folder and choose Add Existing Item. Browse to the common folder, select the desired files, click the drop-down arrow next to the "Add" button and choose "Add as Link".
You may get a source control warning about adding files that are outside of the project directory structure, but this can be ignored since the linked file will be under source control at its source.
Add an AfterBuild Task to Copy Files
When you publish the application to a server the linked files will copied to the project folders to which they are linked and everything works as expected. However, in the development environment the linked files do not physically reside in the project folders. So when you hit F5 to test your application in VS, the shared resources will be missing.
The simple solution is to add an MSBuild task to copy the linked files from their source after each build. This needs to be done to for each project that contains the shared resource links.
Right-click the project and choose Unload Project. Right-click the project again and choose Edit <ProjectFileName>. Scroll to the bottom and add the following (just above "</Project>"):
<Target Name="AfterBuild">
<!-- Copy linked content files to local folders so that they are available in the debugger.
This is only an issue when running the application locally. The linked files should
be automatically published to the correct folder when publishing to a web server. -->
<Copy SourceFiles="%(Content.Identity)"
DestinationFiles="%(Content.Link)"
SkipUnchangedFiles='true'
OverwriteReadOnlyFiles='true'
Condition="'%(Content.Link)' != ''" />
</Target>
** Copy task adapted from this link.
Save the project file then right-click and choose Reload Project.
Related
I try to reference a collection of c# code to a console project in visual studio.
I want to keep the referenced code outside the console project folder.
I want the code to automatically update when it gets changed in the external position.
The referenced codes folder structure should stay intact. (~100 files)
Is there a way of referencing/linking the code without updating everytime? "Include in Project" only works if the code is inside a solution folder.
You can add a "link" to code files outside of your project. This doesn't make a copy of the files.
Right-click your project -> Add -> Existing Item..., and browse to the file(s). Then click the down-arrow on the "Add" button and select "Add As Link":
Linked files appear with a blue arrow in the Solution Explorer:
If you want to reference an entire folder structure of code, you'll need to edit your .csproj. Something like:
<ItemGroup>
<Compile Include="..\SomeDir\**\*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)"/>
</ItemGroup>
Adjust ..\SomeDir to be the path to your code. The **\*.cs is of course a pattern to recursively include all .cs files. %(RecursiveDir), %(Filename), and %(Extension) are MSBuild placeholders.
I organise my git repos in a flat structure:
core
shared1
website1
website2
Where websites 1 and 2 both reference core and shared1.
When I add project references to core and shared1 from websites 1 and 2, they remain intact because they are both in the same relative location from the point of view of the websites.
Additionally, I organise my solution to mirror the external repo configuration, e.g.
core (solution folder)
corelibrary.csproj
shared1 (solution folder)
sharedlibrary.csproj
website1services.csproj
website1.csproj
I found a good solution, but I am curious if there is a better one. I am on Windows and using a simbolic link
cmd /c mklink /d /j _LinkedCode ..\..\..\_Code
then I can use "Include in Project" in visual studio
I'm working on some UWP-app using Visual Studio 2019 Community Edition and added a directory with lots of files as assets. Those files need to be deployed with the application while preserving their directory structure and are no language or screen related resources, but YAML files for some different purpose.
What I did was simply dragging and dropping my folder if interest into the VS-GUI onto the Assets folder, which made the folder available in the project. The problem is that afterwards one needs to set the build options for all those added files. While one can mark all files and set their build options at once, this still is tedious with lots of files in lots of directories.
Making things worse, my added assets change from time to time, folders and files get removed entirely, renamed, new ones added etc. I don't want to manually keep track of those changes, so the only solution I currently have would be to remove my assets from the project, add them again and once again mark all files in all folders and set their build options.
So, instead of marking individual files, can build options be set per-directory for all files underneath?
Any other ideas on how to better handle my use case?
I am new in mvc and c# and I can't solve following problem:
I am trying to create a folder named "Items" in solution folder.
I have tryed to use CreateDirectory method:
Directory.CreateDirectory("~/Images");
But it didn't work for me - folder wasn't created ..
Partly working solution was to create a folder by :
Directory.CreateDirectory(Server.MapPath("~/Images"));
"Items" folder was created, but it is not included in the solution:
How to create folder in solution directory so that it is included in project ?
(I needs to by done by code not by hand)
You need to understand what solution and csproj file is used for
In general, they're being designed and used for development with Visual Studio, and once the project is compiled, all these files will be ignored and excluded from the deployment package
Directory.CreateDirectory(Server.MapPath("~/Images"));
The code above simply create the directory if not existed yet in the deployment package at run-time, so you won't see it in your solution unless you run the project locally (either debug/release mode, it does not matter here). However, everything will run normally in hosted environment (ex: IIS).
For your information, here's the brief of what solution and csproj is
solution (.sln) file: contains information to manage one or many individual projects, contains build environments (for each project), start up mode (useful when you want to start multiple projects in one run), project dependencies and so on. Take a note that VS also read from suo file (solution user options) which is used to defined user-custom preferences (you should not include the .suo file in the version control, because it's custom settings)
csproj file: define the structures of project, what the namespace is, what is static folders, embedded resources, references, packages, etc.
Lastly, if you create the folder manually, VS will auto include that folder into deployment package AND csproj, but depends on the file type, you might need to change the Build Action and Copy To Output Directory in file properties.
Hope it helps.
A deployed web application on a web server doesn't have any notion of Visual Studio solution or projects. So the Directory.CreateDirectory(Server.MapPath("~/Images")) is the correct way to create a folder inside your web application at runtime but we cannot be talking about including it into a solution because this hardly makes sense in a pre-compiled web application. If you create the directory on your local development machine, you could always manually include the folder to the corresponding .csproj file, but at runtime this will not make any difference whatsoever.
The reason I wanted to create a folder (if didn't exist) was to make sure it exits before I try to store image in it.
After reading posts here and a few google searches I have concluded that the proper way to handle image upload would be
To create (In my case) folder "Images" by hand to be sure it exists
Then storing uploaded img in existing folder:
string path =Server.MapPath("~/Images/"+ UploadedImageName);
file.SaveAs(path);
I'd like to add files to a web csproj that are located under its directory, but in a different directory structure than the physical one. Example physical directory structure:
MyProject.csproj
Assets
Settings.config
Now my project needs the following structure when I publish the project (i.e. on the server when deployed) and would be nice if this structure would be visible from Visual Studio too:
MyProject.csproj
Config
Settings.config
Notice that Settings.config isn't in the Assets but in the Config folder.
I don't want to copy the files over, I can't move them and I can't change where these files are loaded from.
Is it possible for such files to be located in the Assets folder while
from VS they appear to be in the Config folder,
and when the project is published they are copied to the Config folder.
The latter one is possible with post-build tasks I think and I believe I can implement it. However I'd like to have the whole development experience, including the folder structure in VS, to show the file being under Config. Is this possible?
I'm looking for something like solution folders, but for projects. Adding files as links would work great, but since the files are in the physical folder of the project I get the error "Cannot add a link to a file that is inside the project tree".
I've got an application that I'm moving over to ClickOnce and the app has a moderately sized data folder with hundreds of files that I need to inlcude in the deployment. The folder needs to be in the same place relative to the EXE after deployment. I've seen several suggestions on how to do this but there doesn't seem to be a agreed upon method for doing this.
Any suggestions would be great -
Thanks!
One good way of doing this is:
Create a folder under the app in VS name e.g. "datafiles"
Add all files to that folder using Add as link in the dialog box after selecting Add existing item on the folder
Mark all files as Copy if newer (Copy to output directory property)
Make sure the build action is content
--> when you publish the files will be put in that folder and be a part of the application installation
Good luck!
After deployment, all files marked as data are placed in the ApplicationDeployment.DataDirectory folder. I know of no way to change this. You could copy the data files during the first run of your app, but this approach will not survive any upgrades that include data file changes.
Alternatively if you have control over the location of the data folder during development, you can place in the same relative (to the app folder) location as will be specified after deployment.
https://msdn.microsoft.com/en-us/library/kzy0fky2.aspx
https://msdn.microsoft.com/en-us/library/6fehc36e.aspx
These two articles provide methods of doing this. Between the two of thing you can find one that works for you. The one that worked for me was:
With a project selected in Solution Explorer, on the Project menu,
click Properties. Click the Publish tab. Click the Application Files
button to open the Application Files dialog box. In the Application
Files dialog box, select the file that you wish to mark as data. In
the Publish Status field, select Data File from the drop-down list.