繁体   English   中英

避免使用新的csproj格式设置“复制到输出目录”

[英]Avoid to set Copy To Output Directory with new csproj format

我使用“新” csproj格式创建了VS2017解决方案,它是使用Nancyfx编写的Web .net 4.6.1应用程序,可通过owin和IIS服务页面。

我所有的视图都在“ Views文件夹下,但如果我未设置“ Copy to Output选项,IIS将找不到它们(对于我的自定义配置文件也是如此)。

我认为问题是输出目录自动设置为bin\\net461\\win7-x86而不是简单的bin ,Web应用程序的工作目录设置为输出目录,因此我必须将copy to output设置copy to output选项。

如何使用新的csproj格式,但保留编辑视图文件的能力而无需重建应用程序? 我已经尝试将输出文件夹设置为bin但是它将被忽略。 我还尝试在“调试”选项卡中设置“工作目录”值,但无济于事。 我在每个配置(调试/发行版)中设置值,所以这不是问题。

这是我的csproj文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <RuntimeIdentifier>win7-x86</RuntimeIdentifier>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" />

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Owin" Version="1.1.2" />
    <PackageReference Include="MongoDB.Driver" Version="2.4.4" />
    <PackageReference Include="Nancy" Version="2.0.0-clinteastwood" />
    <PackageReference Include="Nancy.Authentication.Forms" Version="2.0.0-clinteastwood" />
    <PackageReference Include="Nancy.Authentication.Stateless" Version="2.0.0-clinteastwood" />
    <PackageReference Include="Nancy.Bootstrappers.Unity" Version="2.0.0-clinteastwood" />
    <PackageReference Include="Nancy.Serialization.JsonNet" Version="2.0.0-clinteastwood" />
    <PackageReference Include="Nancy.Validation.FluentValidation" Version="2.0.0-clinteastwood" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="Views\*.sshtml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Configuration" />
  </ItemGroup>

</Project>

经过两天的尝试,我发现了问题的根本原因:这不是VS / csproj问题,而是Nancyfx 2.0配置问题。

对于这里感兴趣的每个人,都是我按照以下步骤解决问题的步骤:

  • 实现一个IRootPathProvider ,该IRootPathProvider配置为返回从Startup类获取的IHostingEnvironment.ContentRootPath
  • 在覆盖IRootPathProvider RootPathProvider { get; }Bootstrapper注册自定义IRootPathProvider IRootPathProvider RootPathProvider { get; } IRootPathProvider RootPathProvider { get; }
  • 在Nancyfx 2.0中,默认情况下,即使在调试模式下,视图也始终被缓存( 请参阅此票证 ),因此在Bootstrapper重写如下所示的configure方法。

方法Bootstrapper.Configure

public override void Configure(INancyEnvironment environment)
{
    base.Configure(environment);
#if DEBUG
    // Disable view caching in debug mode
    environment.Views(true, true);
#endif
}

现在,视图是从VS项目目录(而不是bin文件夹)中读取的,并且不会被缓存,因此在每次视图更改之间不再进行任何重建。 在您的csproj您可以轻松放置:

<None Include="Views\**\*.sshtml" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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