我在一个解决方案中有几个MVC项目。
遵循这个接受的答案:"Add as Link" for folders in Visual Studio projects,我试图通过在基础项目中放置可重用的视图来重用这些项目中的.cshtml视图。到目前为止,我已经在"消费"中设置了位置格式。视图引擎的项目如下:
var locationFormats = new string[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Common/{1}/{0}.cshtml",
"~/Views/Common/Shared/{0}.cshtml"
};
我已经在消费项目中建立了一个递归的链接目录' .csproj
个文件包含以下常见视图:
<ItemGroup>
<Content Include="..\..\..\common.cms\CommonViews\**\*.*">
<Link>\Views\Common\%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
...所有视图都出现在使用链接文件/ cshtml(@)图标的消费项目中,因此路径是正确的。例如,我的测试消费项目中非常清楚地显示以下文件:
\Views\Common\Home\Index.cshtml
但是当我运行这个项目时,我得到了:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.cshtml
~/Views/Shared/Index.cshtml
~/Views/Common/Home/Index.cshtml
~/Views/Common/Shared/Index.cshtml
问题
所以我想知道:甚至可以用这种方式链接视图吗?缺少什么,或者什么可能阻止这种工作?
更一般地说,我想分享的任何非编译文件怎么样?这种技术永远不会起作用吗?
答案 0 :(得分:1)
我发现这是视觉工作室中的已知错误:Linked files in Web Application projects are not deployed properly。这可以追溯到VS-2008,但仍然影响VS-2010和2012(不确定2013年)。
此博客文章详细介绍了一种解决方法: Using Linked Files with Web Application Projects。
如果您只想要解决方案的解决方案,只需将下面的代码粘贴到项目的.csproj
文件的末尾(就在关闭</project>
标记之前)。这将解决任何链接的未编译文件的问题:
<!--
============================================================
_CopyWebApplication
MODIFIED: Ignores linked files as part of normal deployment logic.
This target will copy the build outputs along with the
content files into a _PublishedWebsites folder.
This Task is only necessary when $(OutDir) has been redirected
to a folder other than ~\bin such as is the case with Team Build.
============================================================
-->
<Target Name="_CopyWebApplication" Condition="'$(OutDir)' != '$(OutputPath)'">
<!-- Log tasks -->
<Message Text="Copying Web Application Project Files for $(MSBuildProjectName)" />
<!-- Create the _PublishedWebsites\app\bin folder -->
<MakeDir Directories="$(WebProjectOutputDir)\bin" />
<!-- Copy build outputs to _PublishedWebsites\app\bin folder -->
<Copy SourceFiles="@(IntermediateAssembly)"
DestinationFolder="$(WebProjectOutputDir)\bin"
SkipUnchangedFiles="true" />
<Copy SourceFiles="@(AddModules)"
DestinationFolder="$(WebProjectOutputDir)\bin"
SkipUnchangedFiles="true" />
<Copy SourceFiles="$(IntermediateOutputPath)$(_SGenDllName)"
DestinationFolder="$(WebProjectOutputDir)\%(Content.SubFolder)%(Content.RecursiveDir)"
SkipUnchangedFiles="true"
Condition="'$(_SGenDllCreated)'=='true'" />
<Copy SourceFiles="$(IntermediateOutputPath)$(TargetName).pdb"
DestinationFolder="$(WebProjectOutputDir)\bin"
SkipUnchangedFiles="true"
Condition="'$(_DebugSymbolsProduced)'=='true'" />
<Copy SourceFiles="@(DocFileItem)"
DestinationFolder="$(WebProjectOutputDir)\bin"
SkipUnchangedFiles="true"
Condition="'$(_DocumentationFileProduced)'=='true'" />
<Copy SourceFiles="@(IntermediateSatelliteAssembliesWithTargetPath)"
DestinationFiles="@(IntermediateSatelliteAssembliesWithTargetPath->'$(WebProjectOutputDir)\bin\%(Culture)\$(TargetName).resources.dll')"
SkipUnchangedFiles="true" />
<Copy SourceFiles="@(ReferenceComWrappersToCopyLocal); @(ResolvedIsolatedComModules); @(_DeploymentLooseManifestFile); @(NativeReferenceFile)"
DestinationFolder="$(WebProjectOutputDir)\bin"
SkipUnchangedFiles="true" />
<!-- copy any referenced assemblies to _PublishedWebsites\app\bin folder -->
<Copy SourceFiles="@(ReferenceCopyLocalPaths)"
DestinationFolder="$(WebProjectOutputDir)\bin"
SkipUnchangedFiles="true" />
<!-- MODIFICATION HERE: Copy local content files (i.e. non-linked files) recursively to _PublishedWebsites\app\ folder -->
<Copy Condition=" '%(Content.Link)' == '' "
SourceFiles="%(Content.Identity)"
DestinationFolder="$(WebProjectOutputDir)\%(Content.RelativeDir)" />
</Target>
<!--
============================================================
CopyLinkedContentFiles
A new target to copy any linked content files into the
web application output folder.
NOTE: This is necessary even when '$(OutDir)' has not been redirected.
============================================================
-->
<Target Name="CopyLinkedContentFiles">
<!-- Remove any old copies of the files -->
<Delete Condition=" '%(Content.Link)' != '' AND Exists('$(WebProjectOutputDir)\%(Content.Link)') "
Files="$(WebProjectOutputDir)\%(Content.Link)" />
<!-- Copy linked content files recursively to the project folder -->
<Copy Condition=" '%(Content.Link)' != '' "
SourceFiles="%(Content.Identity)"
DestinationFiles="$(WebProjectOutputDir)\%(Content.Link)" />
</Target>
<!-- Override the default target dependencies to -->
<!-- include the new _CopyLinkedContentFiles target. -->
<PropertyGroup>
<PrepareForRunDependsOn>
$(PrepareForRunDependsOn);
_CopyWebApplication;
CopyLinkedContentFiles;
_BuiltWebOutputGroupOutput
</PrepareForRunDependsOn>
</PropertyGroup>