Asp.net mvc 我可以使用从其他项目链接的文件夹中的视图吗?

Asp.net mvc 我可以使用从其他项目链接的文件夹中的视图吗?,asp.net-mvc,visual-studio,asp.net-mvc-4,visual-studio-2012,Asp.net Mvc,Visual Studio,Asp.net Mvc 4,Visual Studio 2012,我在一个解决方案中有几个MVC项目 根据这个公认的答案:,我试图通过将可重用视图放置在基础项目中,在这些项目中重用.cshtml视图。到目前为止,我已经在视图引擎的“消费”项目中设置了位置格式,如下所示: var locationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml", "~/Views/Common/{1}/{0}.cshtml", "~/Vie

我在一个解决方案中有几个MVC项目

根据这个公认的答案:,我试图通过将可重用视图放置在基础项目中,在这些项目中重用.cshtml视图。到目前为止,我已经在视图引擎的“消费”项目中设置了位置格式,如下所示:

var locationFormats = new string[]
{
    "~/Views/{1}/{0}.cshtml",
    "~/Views/Shared/{0}.cshtml",
    "~/Views/Common/{1}/{0}.cshtml",
    "~/Views/Common/Shared/{0}.cshtml"
};
我在consuming projects'
.csproj
文件中设置了一个递归链接目录,以包含如下常见视图:

<ItemGroup>
    <Content Include="..\..\..\common.cms\CommonViews\**\*.*">
        <Link>\Views\Common\%(RecursiveDir)%(FileName)%(Extension)</Link>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>
但当我运行项目时,我得到:

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
问题
所以我想知道:是否有可能以这种方式链接视图?缺少了什么,或者什么可能会阻止它工作


更一般地说,我想共享的任何未编译文件呢?这项技术永远不会起作用吗?

我发现这是visual studio中的一个已知错误:。这可以追溯到VS-2008,但仍然影响到VS-2010和2012(不确定2013年)

此博客文章详细介绍了解决方法: .

如果您只是想要修复而不需要解释,只需将下面的代码粘贴到项目的
.csproj
文件的末尾(就在关闭
标记之前)。这将解决任何链接的未编译文件的问题:

 <!--
  ============================================================
  _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>

美元(准备独立开支);
_CopyWebApplication;
复制链接内容文件;
_BuiltWebOutputGroupOutput

不幸的是,对于asp.net core 2.0 mvc,这在visualstudio 2017中没有帮助,如下所述:
 <!--
  ============================================================
  _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>