Visual Studio 2017 csproj .NET Core构建-视图未正确复制 [英] Visual Studio 2017 csproj .NET Core build - views not being copied correctly

查看:73
本文介绍了Visual Studio 2017 csproj .NET Core构建-视图未正确复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在.NET Core中运行的项目,并且正在使用VS2017作为IDE.

I have a project running in .NET Core and I am using VS2017 as IDE.

当我通过Visual Studio 2017构建项目时,它不会自动将Views文件夹和wwwroot文件夹添加到[projectRoot]/bin/Debug/netcoreapp1.1/win10-x64(BuildDir)中的输出中.这意味着,如果我尝试直接从bin文件夹中创建的.exe file运行我的网站,则会收到缺少viewswwwroot的错误消息.如果我手动将这些文件夹复制到BuildDir,则视图将正确加载.

When I build my project through Visual Studio 2017 it does not automatically add the Views folder and the wwwroot folder to the output in [projectRoot]/bin/Debug/netcoreapp1.1/win10-x64(BuildDir). This means that if I try to run my website directly from the .exe file created in the bin folder, I get an error with the missing views and wwwroot. If I manually copy these folders to the BuildDir then the views load correctly.

我可以在.csproj文件中设置以下内容:

This I can setup in my .csproj file with the following:

<Content Update="Views\**">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

然后Views工作,但是现在我的布局文件没有被编译,所以我得到以下信息:

And then the Views work but now my layout file is not being compiled so I get the following:

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - FirstAgenda</title>

    <environment names="Development">
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
        <link rel="stylesheet" href="~/css/site.css"/>
        <link rel="stylesheet" href="~/css/overwrite.css" asp-append-version="true" />
    </environment>
    <environment names="Staging,Production">
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/css/bootstrap.min.css"
              asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
              asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
        <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
        <link rel="stylesheet" href="~/css/overwrite.css" asp-append-version="true" />
    </environment>
</head>

这意味着未正确定向我的根文件夹.而且还告诉我,仅将文件复制到输出目录是不正确的.

This means that my root folder is not being targeted correctly. And also kinda tells me that just copying the files to the output directory is incorrect.

如果我发布应用程序(也没有添加.csproj文件),则以上所有方法均有效.我只有一个项目执行器,我希望能够指向我的网站可执行文件的Debug版本,因为与仅使用VS2017构建项目相比,忘记发布很容易.

All of the above is working if I do a publish of my application (also without the addition to the .csproj file). I just have a project runner that I would like to be able to point to the Debug version of my websites executable file, because it is very easy to forget to do a publish compared to just building the project with VS2017.

我只是不知道从哪里可以得到任何帮助?

I just don't know where to go from this and any help will be appreciated?

添加了精简版的csproj(不起作用):

Added the stripped down version of csproj (which does not work):

<Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.1.0-preview4-final" />
    <PackageReference Include="IdentityServer4" Version="1.3.1" />
    <PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.0" />
    <PackageReference Include="IdentityServer4.EntityFramework" Version="1.0.0" />
    <PackageReference Include="OctoPack" Version="3.5.2" />
    <PackageReference Include="Serilog.Extensions.Logging.File" Version="1.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet">
      <Version>1.1.0-preview4-final</Version>
    </DotNetCliToolReference>
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools">
      <Version>1.0.0-msbuild1-final</Version>
    </DotNetCliToolReference>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools">
      <Version>1.0.0-msbuild1-final</Version>
    </DotNetCliToolReference>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\[OWNPROJECT]" />
    <ProjectReference Include="..\[OWNPROJECT1]" />
  </ItemGroup>
</Project>

还尝试使用dotnet new mvc

而且它也不起作用.

我的dotnet core cli版本是[1.0.1].

My dotnet core cli version is [1.0.1].

我遵循了您概述的步骤.我还尝试使用dotnet new mvc创建一个新项目,并按照您的步骤进行.两者都给了我同样的错误.无法找到appsettings.json.如果再将propertygroup添加到.csproj.这告诉msbuild将appsettings.json和web.config复制到输出中

I followed the steps you outlined. I also tried to create a new project with dotnet new mvc and followed your steps. Both gave me the same errors. It was unable to find the appsettings.json. If I then add a propertygroup to the .csproj. That tells msbuild to copy appsettings.json and web.config to the output

<ItemGroup>
  <Content Update="appsettings.json;web.config">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

然后我可以通过exe启动程序,但是当我访问索引时,然后当我尝试访问其中一个视图时,它会给我:

I can then start the program through the exe, but when I access the index, then when I try to access one of the views it gives me:

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0]
    An unhandled exception has occurred while executing the request
    System.InvalidOperationException: The view 'Index' was not found. The following locations were searched:
  /Views/Home/Index.cshtml
  /Views/Shared/Index.cshtml

更新:

Shaun Lutins的答案似乎对我有用.都带有构建和发布选项.唯一的问题是我遇到了错误:

Shaun Lutins answer seems to be working for me. Both with build and publish options. Only problem was that I was getting an error:

Duplicate 'Content' items were included. The .NET SDK includes 'Content' items from your project directory by default.

但是通过更改以下内容解决了该问题:

But it was resolved by changing the following:

<ItemGroup>                                                                            
   <Content Include="appsettings.json" CopyToOutputDirectory="Always" />
   <Content Include="Views\**\*" CopyToOutputDirectory="Always" />
   <Content Include="wwwroot\**\*" CopyToOutputDirectory="Always" />
</ItemGroup>      

<ItemGroup>                                                                            
   <Content Update="appsettings.json;web.config" CopyToOutputDirectory="PreserveNewest"/>
   <Content Update="Views\**\*" CopyToOutputDirectory="PreserveNewest" />
   <Content Update="wwwroot\**\*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>      

推荐答案

我所理解的挑战是运行dotnet build并能够从任何地方执行已编译的EXE.需要两个步骤:

The challenge as I have interpreted it is to run dotnet build and be able to execute the compiled EXE from anywhere. The answer requires two steps:

  1. 将所需内容复制到垃圾箱,然后
  2. 相对于垃圾箱设置ContentRoot和WebRoot.

首先,修改csproj以将所需的内容复制到bin中.开箱即用的dotnet build不会将Views,appsettings.json或wwwroot复制到垃圾箱.因此,我们需要显式指定CopyToOutputDirectory;为此,我们还需要将EnableDefaultContentItems设置为false,否则我们的显式设置将复制默认的内容项设置.

First, modify the csproj to copy required content to the bin. Out of the box, dotnet build does not copy Views, appsettings.json, or wwwroot to the bin. So, we need to specify CopyToOutputDirectory explicitly; and to do that, we also need to set EnableDefaultContentItems to false, otherwise our explicit settings will duplicate the default content item settings.

第二,修改Program.cs以指定新的ContentRoot和WebRoot.开箱即用,Program.cs设置UseContentRoot(Directory.GetCurrentDirectory()).对于我们的情况存在问题,当前目录是我们从中运行可执行文件的目录.因此,如果我们从桌面上的命令行运行EXE,则当前目录将为C:/Users/MyUser/Desktop.该应用程序找不到Views,appsettings.json和其他内容.

Second, modify Program.cs to specify the new ContentRoot and WebRoot. Out of the box, Program.cs sets UseContentRoot(Directory.GetCurrentDirectory()). Problematically for our situation, the current directory is the directory from which we run the executable. So, if we run the EXE from the command line at our desktop, then the current directory will be C:/Users/MyUser/Desktop. The app will not find the Views, appsettings.json, and other content.

以下是我在解释挑战时曾经使用过的一些工作步骤.

Here are some working steps that I have used to meet the challenge as I have interpreted it.

  1. cd C:\temp
  2. dotnet new mvc
  3. 使用正在工作的csproj" XML更新temp.csproj.
  4. 使用正在运行的Program.cs"代码更新Program.cs.
  5. dotnet restore
  6. dotnet build
  7. cd ..
  8. 任何地方运行C:\temp\bin\Debug\netcoreapp1.0\win10-x64\temp.exe
  1. cd C:\temp
  2. dotnet new mvc
  3. Update the temp.csproj with the "Working csproj" XML.
  4. Update the Program.cs with the "Working Program.cs" code.
  5. dotnet restore
  6. dotnet build
  7. cd ..
  8. From anywhere, run C:\temp\bin\Debug\netcoreapp1.0\win10-x64\temp.exe

正在工作的csproj

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

  <PropertyGroup>                                                                        
    <TargetFramework>netcoreapp1.0</TargetFramework>                                     
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>                                     
    <OutputType>exe</OutputType>                                                         
    <EnableDefaultContentItems>false</EnableDefaultContentItems>
  </PropertyGroup>                                                                       

  <ItemGroup>                                                                            
     <Content Include="appsettings.json" CopyToOutputDirectory="Always" />
     <Content Include="Views\**\*" CopyToOutputDirectory="Always" />
     <Content Include="wwwroot\**\*" CopyToOutputDirectory="Always" />
  </ItemGroup>                                                                           

  <ItemGroup>                                                                            

     <!-- Package references omitted for clarity -->

  </ItemGroup>                                                                           

</Project>  

Working Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        // C:\temp\bin\Debug\netcoreapp1.0\win10-x64\temp.dll            
        var assemblyFilePath = 
            System.Reflection.Assembly.GetEntryAssembly().Location;

        // C:\temp\bin\Debug\netcoreapp1.0\win10-x64\
        var binDirectory = 
            System.IO.Path.GetDirectoryName(assemblyFilePath);

        Console.WriteLine(assemblyFilePath);
        Console.WriteLine(binDirectory);

        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(binDirectory) // <--
            .UseWebRoot("wwwroot") // <--
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

这篇关于Visual Studio 2017 csproj .NET Core构建-视图未正确复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆