ASP.NET Core application debugging without publishing on IIS

后端 未结 4 863
再見小時候
再見小時候 2020-12-30 11:49

I used to use asp.net mvc4 and in IIS my website\'s physical path would point my solution directory, and every time I update my code, I just re-build my solution and then I

4条回答
  •  说谎
    说谎 (楼主)
    2020-12-30 12:41

    You don't need to run .Net Core in IIS to get easy debugging etc like we used to do as you described.

    With .Net Core you can just open a command line at your project root and type "dotnet run"

    DotNet Run uses environment variables to drive what it does. So if you want your site to run on a specific URL or port you Type:

    SET ASPNETCORE_URLS=http://example.com
    

    Or if you just want it to run on a different port

    SET ASPNETCORE_URLS=http://localhost:8080
    

    Then to set the Environment

    SET ASPNETCORE_ENVIRONMENT=Development
    

    Once all your environment variables are set, you type

    dotnet run
    

    Now to debug it, you attach to cmd.exe with dotnet run in it's Title. You'll be able to debug your code that way.

    Now, if you are using Visual Studio There is a file called "launchSettings.JSON" under Properties in your project. You can configure profiles here and I have my default profiles set to Kestrel Development and then Kestrel Production, with IIS dead last so that I don't F5 run in IIS Express.

    My LaunchSettings.json looks like this:

    {
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:56545/",
          "sslPort": 0
        }
      },
      "profiles": {
        "Kestrel Development": {
          "executablePath": "dotnet run",
          "commandName": "Project",
          "commandLineArgs": "dotnet run",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development",
            "ASPNETCORE_URLS": "http://localhost:8080"
          }
        },
        "Kestrel Production": {
          "commandLineArgs": "dotnet run",
          "commandName": "Project",
          "environmentVariables": {
            "ASPNETCORE_URLS": "http://localhost:8080",
            "ASPNETCORE_ENVIRONMENT": "Production"
          },
          "executablePath": "dotnet"
        },
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    The first Profile is what F5 uses when you press it. So when I press F5 Visual Studio launches dotnet run for me and set's the Environment and URLS as specified by the environmentVariables section of my profile in launchSettings.JSON.

    Now because I have multiple Profiles I get a drop down next to the run button so I can select Kestrel Production if I want to run in Production mode locally.

提交回复
热议问题