Can the new csproj file structure be used with a ASP.NET Framework project?

前端 未结 2 855
忘掉有多难
忘掉有多难 2020-12-13 02:28

The new .csproj format includes some significant improvements over the classic files, including tight integration with NuGet package management and significantly less-verbos

2条回答
  •  没有蜡笔的小新
    2020-12-13 02:55

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

    • Using the new .Csproj without .Net core #1688
    • Add support for ASP.NET (non-Core) projects #2670
    • Support for "classic" ASP.NET #1978

    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:

      
      
        
          net461
        
      
        
          false
          bin\
        
      
        
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
        
      
        
          
        
      
        
          
        
      
        
          
            Global.asax
          
        
      
        
          
            Designer
          
          
            Web.config
            Designer
          
        
      
      
      

      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)

提交回复
热议问题