简体   繁体   English

ASP.NET 核心应用程序调试,无需在 IIS 上发布

[英]ASP.NET Core application debugging without publishing on IIS

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 can use "Attach to process" (w3wp) to start debugging.我曾经使用 asp.net mvc4 和 IIS 我的网站的物理路径会指向我的解决方案目录,每次我更新我的代码,我只是重新构建我的解决方案,然后我可以使用“附加到进程”(w3wp)开始调试.

In asp.net core, when I publish my website to file system, I can run my website using IIS with no-managed code.在 asp.net 内核中,当我将我的网站发布到文件系统时,我可以使用 IIS 和非托管代码运行我的网站。 But when I point my IIS Website to my solution code of website, it shows 502 error.但是当我将我的 IIS 网站指向我的网站解决方案代码时,它显示 502 错误。

You don't need to run .Net Core in IIS to get easy debugging etc like we used to do as you described.您不需要像我们以前描述的那样在 IIS 中运行 .Net Core 来轻松调试等。

With .Net Core you can just open a command line at your project root and type "dotnet run"使用 .Net Core,您只需在项目根目录中打开命令行并键入“dotnet run”

DotNet Run uses environment variables to drive what it does. DotNet Run 使用环境变量来驱动它的工作。 So if you want your site to run on a specific URL or port you Type:因此,如果您希望站点在特定 URL 或端口上运行,请键入:

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.现在要调试它,您将附加到 cmd.exe 并在其标题中运行 dotnet。 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.现在,如果您使用的是 Visual Studio,在您的项目的 Properties 下有一个名为“launchSettings.JSON”的文件。 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.您可以在此处配置配置文件,我将默认配置文件设置为 Kestrel 开发,然后是 Kestrel 生产,IIS 排在最后,这样我就不会在 IIS Express 中运行 F5。

My LaunchSettings.json looks like this:我的 LaunchSettings.json 如下所示:

{
  "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.第一个配置文件是按下它时 F5 使用的内容。 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.因此,当我按 F5 时,Visual Studio 会为我启动 dotnet run,并按照我的配置文件的 environmentVariables 部分在 launchSettings.JSON 中指定的方式设置环境和 URL。

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.现在因为我有多个配置文件,所以我在运行按钮旁边有一个下拉菜单,如果我想在本地以生产模式运行,我可以选择 Kestrel 生产。

在此处输入图片说明

Simple answer : when you do publish, you call a script that launches the publish-iis tool (see script section in project.json ).简单的回答:当您发布时,您调用一个启动publish-iis工具的script (请参阅project.json script部分)。


In your project you have a web.config file with something like this:在您的项目中,您有一个web.config文件,内容如下:

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" 
 stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/

As you see, there are placeholders "%LAUNCHER_PATH%" and %LAUNCHER_ARGS% parameters.如您所见,有占位符"%LAUNCHER_PATH%"%LAUNCHER_ARGS%参数。 Keep these in mind.记住这些。

Now open your project.json file and you will see a "scripts" section looking something like this:现在打开您的 project.json 文件,您将看到一个“脚本”部分,如下所示:

"scripts":
{  
    "postpublish":"dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"
}

It tells dotnet to run the publish-iis tool after the application is published.它告诉 dotnet 在应用程序发布后运行 publish-iis 工具。 How it works : 它是如何工作的

publish-iis tool goes to the folder where the application was published (not your project folder) and checks if it contains a web.config file. publish-iis 工具转到发布应用程序的文件夹(不是您的项目文件夹)并检查它是否包含 web.config 文件。 If it doesn't, it will create one.如果没有,它将创建一个。 If it does, it will check what kind of application you have (ie whether it is targeting full CLR or Core CLR and – for Core CLR – whether it is a portable or standalone application) and will set the values of the processPath and arguments attributes removing %LAUNCHER_PATH% and %LAUNCHER_ARGS% placeholders on the way.如果是,它将检查您拥有哪种类型的应用程序(即它是针对完整 CLR 还是 Core CLR 以及 – 对于 Core CLR – 它是可移植的还是独立的应用程序)并设置 processPath 和 arguments 属性的值在途中删除 %LAUNCHER_PATH% 和 %LAUNCHER_ARGS% 占位符。

Follow these steps to be able to achieve what you want.按照以下步骤可以实现您想要的。

  1. In launchSettings.json, add a property named iis under iisSettings , like so:在launchSettings.json,添加属性名为iisiisSettings ,就像这样:

     "iis": { "applicationUrl": "http://my.aspnetcoreapp.com" }
  2. Under the profiles section, add a new profile having commandName set to IIS .profiles部分下,添加一个将commandName设置为IIS的新配置文件。 I am calling mine Local IIS .我打电话给我的Local IIS This will add a new option to the Run drop down named Local IIS .这将向名为Local IIS的 Run 下拉菜单添加一个新选项。

     "Local IIS": { "commandName": "IIS", "launchBrowser": true, "launchUrl": "http://my.aspnetcoreapp.com", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }
  3. Create a website in IIS.在 IIS 中创建网站。 Set host name to my.aspnetcoreapp.com .将主机名设置为my.aspnetcoreapp.com Also create/use an app pool for this website that has .NET CLR version set to "No Managed Code".还要为此网站创建/使用一个应用程序池,该应用程序池将 .NET CLR 版本设置为“无托管代码”。

  4. Set physical path of that website to the location of your asp.net core project, not the solution, unless of course if you have the project in the same folder as the solution.将该网站的物理路径设置为您的 asp.net 核心项目的位置,而不是解决方案,除非您的项目与解决方案位于同一文件夹中。
  5. Add a loop back entry in the hosts file (for Windows C:\\Windows\\System32\\drivers\\etc\\hosts )在 hosts 文件中添加一个环回条目(对于 Windows C:\\Windows\\System32\\drivers\\etc\\hosts

    127.0.0.1 my.aspnetcoreapp.com 127.0.0.1 my.aspnetcoreapp.com

  6. Go back to Visual Studio, and run the application.返回到 Visual Studio,并运行应用程序。 Make sure you have "Local IIS" profile selected from Run drop-down.确保从“运行”下拉列表中选择了“本地 IIS”配置文件。 This will launch the application in the browser, at the URL, after a brief loading message that says "Provisioning IIS..." .在显示“Provisioning IIS...”的简短加载消息后,这将在浏览器中的 URL 处启动应用程序。

  7. Done!完成! From now on, you can launch your application at that URL, and can also debug by attaching to process w3wp .从现在开始,您可以在该 URL 上启动您的应用程序,也可以通过附加到进程w3wp 进行调试。

You could also host your app under "Default Web Site" , by specifying the ULR like localhost/MyAspNetCoreApp instead of my.aspnetcoreapp.com .您还可以通过指定localhost/MyAspNetCoreApplocalhost/MyAspNetCoreApp而不是my.aspnetcoreapp.com“默认网站”下托管您的应用程序。 If you do that, a new app pool will be created with the name MyAspNetCoreApp AppPool .如果这样做,将创建一个名为MyAspNetCoreApp AppPool的新应用程序池。

My medium article about this. 关于这个的中篇文章

Why need this?为什么需要这个? This process will help while continuous development & debugging of the code, Here We do not need to deploy the application again & again and don't require to press F5 to run the application, Just change the code & build you can see the application working with the recent changes.此过程将有助于代码的持续开发和调试,这里我们不需要再次部署应用程序,也不需要按 F5 来运行应用程序,只需更改代码并构建您就可以看到应用程序正在运行随着最近的变化。

Description:描述:

  1. Prerequisite: Install the ASP.NET Core Runtime(Hosting Bundle) from the official Microsoft website.先决条件:从微软官方网站安装 ASP.NET Core Runtime(Hosting Bundle)。

    • Search for the.Net core Hosting bundle and choose Microsoft's official site.搜索 .Net core Hosting bundle 并选择微软官方网站。
    • Download the suitable version from the list从列表中下载合适的版本
  2. Create the.Net core Web applications and made the following changes.创建.Net core Web应用并进行如下修改。

    • Go to the folder > Properties > launchsettings.json > open the file and edit as below. Go 到文件夹 > 属性 > launchsettings.json > 打开文件并编辑如下。
    • Add the below section under the existing "profiles" JSON.在现有的“配置文件”JSON 下添加以下部分。 "IIS": { "commandName": "IIS", "launchBrowser": true, "launchUrl": "http://localhost:5000", //ie whatever the port set for iis "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } "IIS": { "commandName": "IIS", "launchBrowser": true, "launchUrl": "http://localhost:5000", //即为 iis 设置的端口 "environmentVariables": { "ASPNETCORE_ENVIRONMENT" : “发展” } }
    • Now, change the "iisSettings" as below.现在,如下更改“iisSettings”。 "windowsAuthentication": false, "anonymousAuthentication": true, "iis": { "applicationUrl": "http://localhost:5000", "sslPort": 0 } "windowsAuthentication": false, "anonymousAuthentication": true, "iis": { "applicationUrl": "http://localhost:5000", "sslPort": 0 }
  3. Go to the Solution explorer > Select the Project > "Alt + Enter" or "Right Click and Select Properties" - Go to the Debug > General > Open debug launch profiles UI. Go to the Solution explorer > Select the Project > "Alt + Enter" or "Right Click and Select Properties" - Go to the Debug > General > Open debug launch profiles UI. - Select the IIS profile from the list - Make sure the AppURL, URL is correctly set as the IIS profile launch URL (ie http://localhost:5000) - Select the IIS profile from the list - Make sure the AppURL, URL is correctly set as the IIS profile launch URL (ie http://localhost:5000)

  4. Open the IIS > Sites > Add WebSite - Site Name: Any Name of site - Application Pool > Create or select a App pool with .NET CLR Version = No Managed Code - Physical Path > Project folder path location - Port: any port number ie 5000打开 IIS > 站点 > 添加网站 - 站点名称:任何站点名称 - 应用程序池 > 创建或 select 应用程序池 .NET 50 文件夹物理路径位置 - 0 > 任何端口号

  5. Build the application and run the URL - http://localhost:5000构建应用程序并运行 URL - http://localhost:5000

  6. Now, to Attach & debug the application, Go to the Visual Studio and Press Ctrl+Alt+P现在,要附加和调试应用程序,Go 到 Visual Studio,然后按 Ctrl+Alt+P

    • Find the Process > W3wp.exe ie In the search section enter 'W3'找到进程 > W3wp.exe 即在搜索部分输入“W3”
    • Mark the checkbox checked > Show processes for all users将复选框标记为选中 > 为所有用户显示进程
    • And Press the attach button.并按下附加按钮。
    • Set the debug point.设置调试点。

Note: Visit the official Microsoft site for more detail.注意:访问微软官方网站了解更多详情。 (Debug ASP.NET Core apps section) https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2022 (调试 ASP.NET 核心应用部分) https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2022

只需运行Ctrl + F5 ,您就可以在运行站点时更改代码,而无需重新启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM