gpt4 book ai didi

visual-studio - 无法使用 VS 2019 构建 Web 应用程序

转载 作者:行者123 更新时间:2023-12-04 11:50:40 25 4
gpt4 key购买 nike

我安装了 VS 2019 Pro 并试图编译我们的解决方案只是为了得到这个:

error MSB4226: The imported project "C:\Program Files (x86)\Microsoft
Visual Studio\2019\Professional\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" was not found. Also, tried
to find "WebApplications\Microsoft.WebApplication.targets" in the fallback search path(s) for $(VSToolsPath) - "C:\Program Files (x86)\MSBuild\Mi
crosoft\VisualStudio\v15.0" . These search paths are defined in "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\
Bin\MSBuild.exe.Config". Confirm that the path in the <Import> declaration is correct, and that the file exists on disk in one of the search paths

这就是它发生的原因:
  • 我们的项目都包含以下导入语句:<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.VisualStudioVersion.v*.Common.props" Condition="'$(VSToolsPath)' == ''" />
  • MSBuildExtensionsPath = C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild
  • MSBuildToolsVersion = Current
  • 文件夹$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)确实存在,见下文。
  • $(MSBuildExtensionsPath)\$(MSBuildToolsVersion) :
    C:\> dir "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current"


    Directory: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current


    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    d----- 6/13/2019 9:11 PM Bin
    d----- 5/27/2019 4:42 PM Imports
    d----- 5/27/2019 4:41 PM Microsoft.Common.targets
    d----- 5/27/2019 4:42 PM SolutionFile
    -a---- 5/27/2019 4:38 PM 13606 Microsoft.Common.props
    -a---- 5/27/2019 4:38 PM 789 Microsoft.VisualStudioVersion.v15.Common.props
    -a---- 5/27/2019 4:38 PM 2029 THIRDPARTYNOTICES.txt


    C:\>

    所以,VS 2019 实际上加载了 Microsoft.VisualStudioVersion.v15.Common.props .里面是什么?观察:
    C:\> cat "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Microsoft.VisualStudioVersion.v15.Common.props"
    <!--
    ***********************************************************************************************
    Microsoft.VisualStudio.v15.Common.props

    WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
    created a backup copy. Incorrect changes to this file will make it
    impossible to load or build your projects from the command-line or the IDE.

    Copyright (C) Microsoft Corporation. All rights reserved.
    ***********************************************************************************************
    -->

    <Project>

    <PropertyGroup>
    <VisualStudioVersion>15.0</VisualStudioVersion>
    <VSToolsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    </PropertyGroup>

    </Project>
    C:\>

    所以,最后 VSToolsPathC:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VisualStudio\v15.0这显然是错误的,因为不存在这样的目录。存在的是 v16.0。

    无论如何,这一切的根源似乎是 c:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild向全世界传达 MSBuild 15 是要使用的版本,而不是 MSBuild 16。

    我错过了什么?

    最佳答案

    Our projects all include the following import statement: <Import
    Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.VisualStudioVersion.v*.Common.props"
    Condition="'$(VSToolsPath)' == ''" />



    我猜问题的根本原因是你 将语句放在 VSToolsPath 的定义之上属性(property) .

    在VS2019中创建一个简单的asp.net web应用(.net框架),其结构如下:
    <Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

    <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    ...
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    ...
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    </PropertyGroup>

    <ItemGroup>
    ...
    </ItemGroup>

    <!--Position 1-->
    <!--<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.VisualStudioVersion.v*.Common.props" Condition="'$(VSToolsPath)' == ''" />--> <!--Position 1-->
    <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> <!--This is where the VSToolsPath is defined.-->
    </PropertyGroup>

    <!--Position 2-->
    <!--<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.VisualStudioVersion.v*.Common.props" Condition="'$(VSToolsPath)' == ''" />--> <!--Position 2-->
    <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
    <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" /> <!--This is where the error finally occurs.-->
    <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
    <ProjectExtensions>
    ...
    </ProjectExtensions>
    <!-- To modify your build process, add your task inside one of the targets below and uncomment it. ...-->
    </Project>

    我假设您可以在 xx.csproj 文件的顶部或上方的某个位置添加该语句
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

    如果是这样,因为现在 $(VSToolsPath) 还没有定义。它的值为空,那么构建系统会执行你语句中的Import 元素来Import $(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.VisualStudioVersion.v*.Common.props文件,这将导致 VisualStudioVersion属性设置为 15.0 而不是 16.0。

    并且因为它从 Microsoft.VisualStudioVersion.v15.Common.props 导入属性, VSToolsPath现在设置为 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0 ,这将导致您得到的最终错误,因为路径的一部分是 Microsoft.WebApplication.targets文件是 $(VSToolsPath)。看:
    <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" /> <!--This is where the error finally occurs.-->

    因此,如果您将语句添加到 $(VSToolsPath) 的第一个定义之上,例如 位置 1 ,你会得到这个错误。如果您在 $(VSToolsPath) 的定义下方和 Import of Microsoft.WebApplication.targets 上方添加您的语句喜欢 位置 2 ,一切正常。

    更新 1:

    关于导入元素的信息见 this document . 导入的项目按照导入的顺序进行检查。

    更新 2:

    来自 的提示标记 ,我在 msbuild 中发布了一个问题。 The link .

    关于visual-studio - 无法使用 VS 2019 构建 Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57856650/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com