简体   繁体   中英

Good way to share views, images, and other content between MVC projects?

I have a couple of MVC4 web sites that use the same layout, images, css files, etc. I am trying to find a way to share these types of resources between them so that when I make a change to a file, both projects get the updates without me having to do anything extra. So far all I can manage to do is a copy-paste between the projects (adding linked items doesn't work because a physical copy of the file is not made) but this obviously won't work in the long run. Is there an easy way to do this, or am I out of luck?

I am also using IISExpress to test the sites so whatever the solution is should be compatible with this.

I have come up with what is possibly the easiest solution to the problem in a few short steps. The most time consuming part is making a copy of the powershell script below, but you only have to do that once, forever.

Part 1
The first step includes linking the files that you want to share amongst your projects. These can be images, css, views, javascript, etc. Use the dialog in 'add existing items' like so:

链接它!

Linking files works right away for code files, but not for content. This is because a physical copy of the file is not created, so the server (IISExpress in this case) will not be able to find it, resulting in a 404, or a crash.

Part 2
I managed to solve this problem by writing a quick powershell script that will make physical copies of linked items in a given project. This is the script that I am using now and while, it is working for all of the file types mentioned above, it may not cover everything in the future, but I think expanding upon it would be rather easy.

function Copy-LinkedFiles
{
<#
   .DESCRIPTION
   Creates a physical copy of linked files in your visual studio projects.
   This is very handy for ASP projects that make sharing resources between projects in a solution a real
   pain in the ass.
#>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $ProjectFile
    )

   # Find all of the linked Content items in the projects.
   $ns = @{p = "http://schemas.microsoft.com/developer/msbuild/2003"}

   $contentFiles = select-xml -Path $ProjectFile -Namespace $ns -XPath "/p:Project/p:ItemGroup/p:Content"

   $withLink = $contentFiles | Select-Object -ExpandProperty Node | Where-Object -Property Link

   $projDir = [System.IO.Path]::GetDirectoryName($ProjectFile)
   $withLink | ForEach-Object -Process {

        $srcPath = [System.IO.Path]::GetFullPath($projDir + "\" + $_.Include)
        $destPath = [System.IO.Path]::GetFullPath($projDir + "\" + $_.Link)

        Write-Verbose "Copying file from $srcPath to $destPath"
        Copy-Item -path $srcPath -Destination $destPath -Force
   }
}

Part 3
Once you have the script available, you can just include a pre-post build event as you see fit like this:

建立

In this case I am using the 'verbose' switch so that I get the copy messages 3from the script in my output window. I have also setup my powershell profile to load the script so that I can call the Cmdlet directly, although you could do it any way you like. The good news is once you are set up, you can just paste the build event, and you are on your way to easily sharing resources between your MVC projects.
Since a physical copy of the linked items are made with each build, you will no longer get those pesky 404 and file not found messages.

yes - use "Add as a link" option of "Add existing item" command:

在此输入图像描述

this way same file can be added to multiple projects WITHOUT creating a copy of it. Symlink if you wish.

if you use SVN (not sure how this is done in other version control systems - but i believe it's similar) - you can use "svn:externals" property to map a folder from your "original" project into other projects. Not sure how MVC will take that - can you put layout file into different from "Shared" folder? I think you should be able to - just need to specify the path properly in your views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM