新的csproj文件结构可以与ASP.NET Framework项目一起使用吗?

Pau*_*ner 27 asp.net csproj asp.net-core-2.0

新的.csproj格式包括对经典文件的一些重大改进,包括与NuGet包管理的紧密集成以及明显不那么冗长的结构.我希望在使用.NET Framework 4.6和ASP.NET的同时获得这些好处(因为我的项目依赖于尚未生成.NET Core版本的Umbraco).

最大的挑战似乎是调试体验 - ASP.NET Core项目期望运行dotnet核心应用程序并为IIS实例设置反向代理.这个过程与.NET Framework模型完全不同,我不知道从哪里开始尝试在Visual Studio中设置调试.

有没有办法让这两个项目模型混合使用?

Cod*_*ler 52

关于支持ASP.NET(非核心)应用程序的新csproj格式,GitHub上存在许多未解决的问题.他们中有一些:

您可能已经了解,ASP.NET应用程序尚不支持新的csproj格式.它可以使它工作,但它不会很顺利.

前段时间我尝试用新的csproj格式创建ASP.NET MVC项目,只是为了好玩.我做到了,但我没有玩过很多次.知道你的经历会很有趣.

步骤如下:

  1. 删除旧的不需要的项目文件:

    • MvcApplication.csproj
    • MvcApplication.csproj.user
    • packages.config
  2. 使用以下内容创建新的MvcApplication.csproj:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>net461</TargetFramework>
      </PropertyGroup>
    
      <PropertyGroup>
        <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
        <OutputPath>bin\</OutputPath>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Antlr" version="3.4.1.9004" />
        <PackageReference Include="bootstrap" version="3.0.0" />
        <PackageReference Include="jQuery" version="1.10.2" />
        <PackageReference Include="jQuery.Validation" version="1.11.1" />
        <PackageReference Include="Microsoft.ApplicationInsights" version="2.2.0" />
        <PackageReference Include="Microsoft.ApplicationInsights.Agent.Intercept" version="2.0.6" />
        <PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" version="2.2.0" />
        <PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.2.0" />
        <PackageReference Include="Microsoft.ApplicationInsights.Web" version="2.2.0" />
        <PackageReference Include="Microsoft.ApplicationInsights.WindowsServer" version="2.2.0" />
        <PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.2.0" />
        <PackageReference Include="Microsoft.AspNet.Mvc" version="5.2.3" />
        <PackageReference Include="Microsoft.AspNet.Razor" version="3.2.3" />
        <PackageReference Include="Microsoft.AspNet.Web.Optimization" version="1.1.3" />
        <PackageReference Include="Microsoft.AspNet.WebPages" version="3.2.3" />
        <PackageReference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.5" />
        <PackageReference Include="Microsoft.CSharp" Version="4.4.1" />
        <PackageReference Include="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" />
        <PackageReference Include="Microsoft.Net.Compilers" version="2.1.0" developmentDependency="true" />
        <PackageReference Include="Microsoft.Web.Infrastructure" version="1.0.0.0" />
        <PackageReference Include="Modernizr" version="2.6.2" />
        <PackageReference Include="Newtonsoft.Json" version="6.0.4" />
        <PackageReference Include="Respond" version="1.2.0" />
        <PackageReference Include="WebGrease" version="1.5.2" />
      </ItemGroup>
    
      <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
      </ItemGroup>
    
      <ItemGroup>
        <Reference Include="System.Web" />
      </ItemGroup>
    
      <ItemGroup>
        <Compile Update="Global.asax.cs">
          <DependentUpon>Global.asax</DependentUpon>
        </Compile>
      </ItemGroup>
    
      <ItemGroup>
        <Content Include="Web.config">
          <SubType>Designer</SubType>
        </Content>
        <Content Include="Web.*.config">
          <DependentUpon>Web.config</DependentUpon>
          <SubType>Designer</SubType>
        </Content>
      </ItemGroup>
    
    </Project>
    
    Run Code Online (Sandbox Code Playgroud)

    上面的长包列表包含为默认ASP.NET MVC应用程序添加的默认包.您应该添加应用程序使用的其他包.

    不要忘记添加Microsoft.CSharp包,否则您将在ViewBag分配时遇到以下编译错误:

    错误CS0656:缺少编译器所需的成员'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

    在ASP.NET项目中,Microsoft.CSharp添加为项目的参考.但最好将它作为NuGet包使用.

    唯一无法避免的直接参考是a System.Web.

  3. 调试项目

    当你说调试可能很痛苦时,你是对的.由于Visual Studio不知道它是一个ASP.NET应用程序,因此没有即时方法来启动调试会话.

    我在这里看到两种可能的解决方

    一个.使用IIS Express进行调试.

    基于IIS Express可执行文件配置调试非常容易.只需创建以下调试配置文件:

    在此输入图像描述

    对应的launchSettings.json:

    {
      "profiles": {
        "ASP.NET Old csproj": {
          "commandName": "Executable",
          "executablePath": "c:\\Program Files\\IIS Express\\iisexpress.exe",
          "commandLineArgs": "/path:\"$(SolutionDir)$(ProjectName)\" /port:12345"
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    湾 使用IIS进行调试.

    在IIS管理器中,创建指向项目目录的应用程序.现在,您可以通过附加到w3wp.exe流程来调试应用程序.

这是GitHub上的示例项目.它基本上是默认的ASP.NET MVC项目,按照上述步骤迁移到新的csproj格式.它可以编译,执行和调试(包括IIS Express的配置文件)

  • 这似乎也打破了网络发布 - 它现在似乎只提供了一个文件夹目标,而不是像WebDeploy这样的东西. (3认同)
  • 不确定我是否正确理解你。是的,ASP.NET Core 改变了静态文件的处理方式,现在所有静态内容都驻留在 [wwwroot](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files) 中。它只是 ASP.NET Core Web Host 默认使用的位置。但是它与 csproj 文件结构无关。并且由于我们讨论 ASP.NET(非核心),`wwwroot` 在这里没有发挥作用。所有 ASP.NET 静态内容文件夹(如`Content`、`fonts`、`Scripts`)在项目目录和部署应用程序目录中都具有相同的相对位置。 (2认同)
  • 有没有人设法通过命令行部署它?`dotnet publish` 似乎没有产生有效的输出。示例项目在 iis express 中运行时给我一个 403 错误。 (2认同)

fre*_*tje 9

我正在关注有关此问题的 github 问题,并且发布的最新评论添加了最后缺失的部分。基本上,使用这样的 .csproj 文件:

<Project>
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
  <PropertyGroup>
    <OutputType>Library</OutputType>
    <OutputPath>bin\</OutputPath>
    <TargetFramework>net48</TargetFramework>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <PublishProfileImported>true</PublishProfileImported>
  </PropertyGroup>
  <ItemGroup>
    <ProjectCapability Include="DotNetCoreWeb" />
    <ProjectCapability Include="SupportsSystemWeb" />
  </ItemGroup>
  <!-- add your packagereference/content/etc includes here -->
  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <!-- order is important! -->
  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" />
</Project>
Run Code Online (Sandbox Code Playgroud)

一切正常!

  • 使用正确的 applicationhost.config 启动 iisexpress 的 Visual Studio 的正常运行和调试体验。如果您在启动设置中填写 sslport,它甚至可以与 ssl 一起使用。
  • 正常的编辑和调试 razor 体验,没有任何红色曲线和有效的智能感知/自动完成。

似乎仍然缺少的唯一一件事是右键单击部署功能......但是当你可以msbuild /p:DeployOnBuild=True用来部署时,我终于看不到任何阻止我升级的东西了。

  • +1 如果这个问题是在 2020 / 2021 年提出的,这应该是公认的答案!希望我先读过它。 (2认同)
  • 我需要添加 `&lt;Reference Include="System.Web" /&gt;` 和 `&lt;PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.7" /&gt;` 来构建我的项目。 (2认同)