今天上午基于.net core做了一个简单的Web Api的Demo,练习一下IIS部署,本以为很简单,没想到遇到了很多坑,折腾了大半天才部署成功,简单记录一下,以供大家参考 。
1、发布项目

文章插图
2、下载并安装AspNetCoreModuleV2模块
下载地址:https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-2.2.2-windows-hosting-bundle-installer

文章插图
下载页面
3、当打开IIS面板,在模块里有AspNetCoreModuleV2时表示安装成功

文章插图

文章插图
4、添加网站并配置发布的路径、ip、端口等信息

文章插图
5、将应用程序池.NET CLR版本选择为无托管代码

文章插图
6、访问时如果报500错误,则先打开发布文件夹里的web.cinfig文件,修改stdoutLogEnabled="true",根据错误日志定位具体错误

文章插图
【.net core IIS部署教程】

文章插图
7、常见问题——数据库连接失败,可能是数据库连接字符串有问题
例如错误提示:You shouldn't use localdb when hosting in IIS. LocalDB is launched in the context of the user running it.(在IIS中托管时不应该使用localdb 。LocalDB是在运行它的用户的上下文中启动的 。)
修改数据库连接字符串:
正确配置:"shopConnectionString": "server=.;database=ShopDB;uid=sa;pwd=123"
错误配置:"Server=PS20190608PCTW;DataBase=ShopDB;Trusted_Connection=True;"(本地可以访问,发布到IIS 不能访问)

文章插图
8、修改完数据库连接字符串后再次访问成功

文章插图
推荐阅读
- 基于.NET Core+Bootstrap的快速后台开发框架
- 基于.NET Core的Orchard Core框架出来了
- .Net在Windows上使用Jenkins做CI/CD
- 初试CoreDNS
- .NET CORE HttpClient使用
- 会声会影视频编辑
- hms core是什么软件可以卸载吗?hms core是什么软件有用吗?
- ASP.NET Core 6.0 添加 JWT 认证和授权
- 如何在树莓派4开发板上使用Windows 10 IoT Core?
- 在.NET和.NET Core应用程序中优雅的实现后台任务——Hangfire