简体   繁体   中英

Avoid to set Copy To Output Directory with new csproj format

I created a VS2017 solution using the "new" csproj format, it's a web .net 4.6.1 application written using Nancyfx, serving pages thru owin and IIS.

All my views are under the Views folder but if I don't set the Copy to Output options IIS cannot find them (the same is true for my custom config files).

The problem, I think, is that the output directory is automatically set to bin\\net461\\win7-x86 instead of simply bin and the working directory of the web app is set to the output dir, so I have to set the copy to output option.

How can I use the new csproj format but retain the ability to edit view files without rebuilding the app? I already tried to set the output folder to bin but it is ignored. I also tried to set the the Working Directory value in the Debug tab but to no avail. I set the values in every configuration (debug/release) so that's not the issue.

This is my csproj file:

<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>

After two days spent trying I found the root cause of the problem: it was not a VS/csproj problem but rather a Nancyfx 2.0 configuration issue.

For everyone interested here are the steps that I followed to fix the problem:

  • Implement an IRootPathProvider configured to return the value of IHostingEnvironment.ContentRootPath as taken from your Startup class
  • Register the custom IRootPathProvider in your Bootstrapper overriding IRootPathProvider RootPathProvider { get; } IRootPathProvider RootPathProvider { get; }
  • By default in Nancyfx 2.0 the view are always cached, even in debug mode ( see this ticket ), so in your Bootstrapper override the configure method as shown below.

Method Bootstrapper.Configure :

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

Now views are read from the VS project directory (and not the bin folder) and are not cached, so no more rebuilds between every view change. In your csproj you can easily put:

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

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