Asp.net
网站如何编译发布
ASP.NET 网站有多种编译发布方式,我来为您详细介绍:
1. Visual Studio 发布
使用发布向导
右键项目 → 发布 → 选择发布目标
发布目标选项:
- 文件夹
- IIS
- Azure
- FTP
发布配置文件
<!-- Properties/PublishProfiles/Release.pubxml -->
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<PublishUrl>C:\Publish\MyWebsite</PublishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
</PropertyGroup>
2. 命令行发布
.NET Core/.NET 5+
# 发布到文件夹
dotnet publish -c Release -o ./publish
# 指定运行时
dotnet publish -c Release -r win-x64 --self-contained
# 单文件发布
dotnet publish -c Release -r win-x64 --self-contained -p:PublishSingleFile=true
.NET Framework
# 使用 MSBuild
msbuild MyWebsite.csproj /p:Configuration=Release /p:Platform="Any CPU" /p:PublishProfile=FolderProfile
3. Web Deploy 发布
配置文件
<Project>
<PropertyGroup>
<WebPublishMethod>MSDeploy</WebPublishMethod>
<MSDeployServiceURL>https://yourserver.com:8172/msdeploy.axd</MSDeployServiceURL>
<DeployDefaultTarget>WebPublishToAzureWebSites</DeployDefaultTarget>
<UserName>$username</UserName>
<Password>password</Password>
<SiteUrlToLaunchAfterPublish>https://yoursite.com</SiteUrlToLaunchAfterPublish>
</PropertyGroup>
</Project>
4. IIS 部署配置
web.config 示例
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\MyWebsite.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</configuration>
5. 发布前的准备工作
生产环境配置
// appsettings.Production.json
{
"ConnectionStrings": {
"DefaultConnection": "Production connection string"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
编译优化
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<Optimize>true</Optimize>
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>
6. Docker 部署
Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyWebsite.csproj", "."]
RUN dotnet restore "MyWebsite.csproj"
COPY . .
RUN dotnet build "MyWebsite.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyWebsite.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyWebsite.dll"]
7. 自动化部署
GitHub Actions 示例
name: Deploy to IIS
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release
- name: Publish
run: dotnet publish --configuration Release --output ./publish
- name: Deploy to IIS
run: |
# 部署脚本
选择哪种方式取决于您的具体需求和部署环境。有什么特定的场景需要详细了解吗?
1 Like