使用PowerShell自动部署ASP.NetCore程序到IIS

asp.net core

安装asp.net core sdk

https://dotnet.microsoft.com/en-us/download/dotnet/3.1
使用PowerShell自动部署ASP.NetCore程序到IIS_第1张图片
使用PowerShell自动部署ASP.NetCore程序到IIS_第2张图片
使用PowerShell自动部署ASP.NetCore程序到IIS_第3张图片

创建asp.net core项目

dotnet new webapi

使用PowerShell自动部署ASP.NetCore程序到IIS_第4张图片
使用PowerShell自动部署ASP.NetCore程序到IIS_第5张图片
运行项目
使用PowerShell自动部署ASP.NetCore程序到IIS_第6张图片
访问https://localhost:5001/WeatherForecast
使用PowerShell自动部署ASP.NetCore程序到IIS_第7张图片

iis配置

安装iis

以管理员身份运行powershell

Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole, IIS-WebServer, IIS-CommonHttpFeatures, IIS-ManagementConsole, IIS-HttpErrors, IIS-HttpRedirect, IIS-WindowsAuthentication, IIS-StaticContent, IIS-DefaultDocument, IIS-HttpCompressionStatic, IIS-DirectoryBrowsing

使用PowerShell自动部署ASP.NetCore程序到IIS_第8张图片

安装hosting bundle

https://dotnet.microsoft.com/en-us/download/dotnet/3.1
使用PowerShell自动部署ASP.NetCore程序到IIS_第9张图片
使用PowerShell自动部署ASP.NetCore程序到IIS_第10张图片
使用PowerShell自动部署ASP.NetCore程序到IIS_第11张图片
使用PowerShell自动部署ASP.NetCore程序到IIS_第12张图片
也可以命令行安装

Invoke-WebRequest -Uri "https://aka.ms/dotnetcore.2.0.0-windowshosting" -OutFile "DotNetCore.WindowsHosting.exe"
Start-Process "DotNetCore.WindowsHosting.exe" -Wait

重启iis服务

Invoke-Expression "net stop was /y"
Invoke-Expression "net start w3svc"

使用PowerShell自动部署ASP.NetCore程序到IIS_第13张图片
可以使用以下命令来检测ASPNetCoreModule是否已安装

# asp.net core 2.0以前
Get-WebGlobalModule -Name AspNetCoreModule -ErrorAction Ignore
# asp.net core 3.1之后
Get-WebGlobalModule -Name AspNetCoreModuleV2 -ErrorAction Ignore

出现如下信息说明安装成功了
使用PowerShell自动部署ASP.NetCore程序到IIS_第14张图片

创建网站

以管理员身份启动powershell

首先我们要引入PowerShell中的WebAdministration模块,这样就可以对IIS进行相关的操作了

Import-Module WebAdministration

创建应用程序池
接下来要创建一个应用程序池,名称为TestApp

New-Item -path IIS:\AppPools\TestApp

使用PowerShell自动部署ASP.NetCore程序到IIS_第15张图片
把应用程序池的.Net版本设置为无托管代码

Set-ItemProperty -Path IIS:\AppPools\TestApp -Name managedRuntimeVersion -Value ''

在这里插入图片描述
使用PowerShell自动部署ASP.NetCore程序到IIS_第16张图片
发布项目

dotnet publish

使用PowerShell自动部署ASP.NetCore程序到IIS_第17张图片
使用PowerShell自动部署ASP.NetCore程序到IIS_第18张图片
创建了应用程序池之后,就要创建一个网站,并使用刚创建的应用程序池TestApp,将网站的名称设置为TestSite,并指向你的网站路径如
C:\Users\Administrator\Desktop\test\bin\Debug\netcoreapp3.1\publish

New-Website -name TestSite -PhysicalPath "C:\Users\Administrator\Desktop\test\bin\Debug\netcoreapp3.1\publish" -ApplicationPool TestApp -Port 8080

使用PowerShell自动部署ASP.NetCore程序到IIS_第19张图片
使用PowerShell自动部署ASP.NetCore程序到IIS_第20张图片
访问https://localhost:8080/WeatherForecast
https的原因
使用PowerShell自动部署ASP.NetCore程序到IIS_第21张图片
使用PowerShell自动部署ASP.NetCore程序到IIS_第22张图片
这行代码搞的鬼,重新发布即可
使用PowerShell自动部署ASP.NetCore程序到IIS_第23张图片
给发布网站的文件夹添加everyone权限
使用PowerShell自动部署ASP.NetCore程序到IIS_第24张图片
使用PowerShell自动部署ASP.NetCore程序到IIS_第25张图片

参考

你可能感兴趣的:(CI,asp.net,服务器,microsoft)