> 文章列表 > 使用PowerShell自动部署ASP.NetCore程序到IIS

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

使用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
使用PowerShell自动部署ASP.NetCore程序到IIS
使用PowerShell自动部署ASP.NetCore程序到IIS

创建asp.net core项目

dotnet new webapi

使用PowerShell自动部署ASP.NetCore程序到IIS
使用PowerShell自动部署ASP.NetCore程序到IIS
运行项目
使用PowerShell自动部署ASP.NetCore程序到IIS
访问https://localhost:5001/WeatherForecast
使用PowerShell自动部署ASP.NetCore程序到IIS

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

安装hosting bundle

https://dotnet.microsoft.com/en-us/download/dotnet/3.1
使用PowerShell自动部署ASP.NetCore程序到IIS
使用PowerShell自动部署ASP.NetCore程序到IIS
使用PowerShell自动部署ASP.NetCore程序到IIS
使用PowerShell自动部署ASP.NetCore程序到IIS
也可以命令行安装

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
可以使用以下命令来检测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

创建网站

以管理员身份启动powershell

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

Import-Module WebAdministration

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

New-Item -path IIS:\\AppPools\\TestApp

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

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

使用PowerShell自动部署ASP.NetCore程序到IIS
使用PowerShell自动部署ASP.NetCore程序到IIS
发布项目

dotnet publish

使用PowerShell自动部署ASP.NetCore程序到IIS
使用PowerShell自动部署ASP.NetCore程序到IIS
创建了应用程序池之后,就要创建一个网站,并使用刚创建的应用程序池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
使用PowerShell自动部署ASP.NetCore程序到IIS
访问https://localhost:8080/WeatherForecast
https的原因
使用PowerShell自动部署ASP.NetCore程序到IIS
使用PowerShell自动部署ASP.NetCore程序到IIS
这行代码搞的鬼,重新发布即可
使用PowerShell自动部署ASP.NetCore程序到IIS
给发布网站的文件夹添加everyone权限
使用PowerShell自动部署ASP.NetCore程序到IIS
使用PowerShell自动部署ASP.NetCore程序到IIS

参考