新的 csproj 文件结构可以用于 ASP.NET Framework 项目吗? [英] Can the new csproj file structure be used with a ASP.NET Framework project?

查看:34
本文介绍了新的 csproj 文件结构可以用于 ASP.NET Framework 项目吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

有什么办法可以让这两种项目模型混合在一起吗?

解决方案

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

  • 对应的launchSettings.json:

    <代码>{个人资料":{ASP.NET 旧 csproj":{"commandName": "可执行文件","executablePath": "c:\Program Files\IIS Express\iisexpress.exe","commandLineArgs": "/path:"$(SolutionDir)$(ProjectName)"/port:12345"}}

    B.使用 IIS 进行调试.

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

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

    The new .csproj format includes some significant improvements over the classic files, including tight integration with NuGet package management and significantly less-verbose structure. I want to gain these benefits whilst still using the .NET Framework 4.6 and ASP.NET (because my project depends on Umbraco which has yet to produce a .NET Core version).

    The biggest challenge would seem to be the debugging experience - an ASP.NET Core project expects to run a dotnet core application and set up a reverse proxy to an IIS instance. This process is completely alien to the .NET Framework model and I wouldn't know where to start trying to set up debugging in Visual Studio.

    Is there any way to get these two project models to mix?

    解决方案

    There is a bunch of open issues on GitHub regarding support of new csproj format for ASP.NET (non-Core) applications. Some of them:

    As you probably already understood, new csproj format is not yet supported for ASP.NET applications. It's possible to make it work, however it won't be smooth.

    Some time ago I have tried to create ASP.NET MVC project in new csproj format, just for fun. I made it work, however I had not played with it a lot. So it will be interesting to know your experience.

    The steps are following:

    1. Remove old unrequired project files:

      • MvcApplication.csproj
      • MvcApplication.csproj.user
      • packages.config
    2. Create new MvcApplication.csproj with the following content:

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

      The long package list above includes default packages added for default ASP.NET MVC application. You should add other packages used by your application.

      Don't forget to add the Microsoft.CSharp package, otherwise you'll get following compilation error on ViewBag assignments:

      error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

      In ASP.NET projects, Microsoft.CSharp is added as reference to the project. But it's better to consume it as NuGet package.

      The only direct reference that could not be avoided is a System.Web.

    3. Debugging the project

      You were right when said that debugging could be a pain. Since Visual Studio does not know it's an ASP.NET application, there is no instant method to start debugging session.

      I see 2 possible solutions here:

      a. Use IIS Express for debugging.

      It's quite easy to configure debugging based on IIS Express executable. Just create the following debugging profile:

      Corresponding launchSettings.json:

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

      b. Use IIS for debugging.

      In IIS Manager create application that points to directory with your project. Now you could debug your application by attaching to w3wp.exe process.

    Here is Sample Project on GitHub. It is basically default ASP.NET MVC project migrated to new csproj format following above steps. It could be compiled, executed and debugged (profile for IIS Express included)

    这篇关于新的 csproj 文件结构可以用于 ASP.NET Framework 项目吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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