加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

基于.NET Core 3.1 网站开发和部署的方法

发布时间:2020-09-15 17:48:49 所属栏目:Asp教程 来源:互联网
导读:这篇文章主要介绍了基于.NET Core 3.1 网站开发和部署的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面

public void ConfigureServices(IServiceCollection services) { //其他代码省略了 ... services.AddDbContext<DBUtility.HotelWebDbContext>( options => options.UseMySql( Configuration.GetConnectionString("HotelWeb"), x => x.ServerVersion("5.5.64-mariadb") ) ); services.AddTransient<INewsManager, NewsManager>(); services.AddTransient<IDishManager, DishManager>(); services.AddTransient<IDishBookManager, DishBookManager>(); services.AddTransient<ISuggestionManager, SuggestionManager>(); services.AddTransient<IRecruitmentManager, RecruitmentManager>(); services.AddTransient<ISysAdminManager, SysAdminManager>(); services.AddTransient<INewsService,NewsService>(); services.AddTransient<IDishService,DishService>(); services.AddTransient<IDishBookService,DishBookService>(); services.AddTransient<ISuggestionService,SuggestionService>(); services.AddTransient<IRecruitmentService,RecruitmentService>(); services.AddTransient<ISysAdminService,SysAdminService>(); }

4.修改代码使用依赖注入

public class HomeController : Controller { private readonly INewsManager newsManager; private readonly ISuggestionManager suggestionManager; private readonly IRecruitmentManager recruitmentManager; public HomeController( INewsManager newsManager, ISuggestionManager suggestionManager, IRecruitmentManager recruitmentManager) { this.newsManager = newsManager; this.suggestionManager = suggestionManager; this.recruitmentManager = recruitmentManager; } // ... }

5.测试

启动项目也没有什么问题

七、项目发布

1.项目配置

dotnet core 中没有Web.conf文件了。
查看文档,都是通过Startup.cs中配置项目的。
暂时放弃配置。

2.命令行发布项目

CLI 提供了发布项目的相关命令

dotnet publish -c Release --no-self-contained -o /path/to/save/project/

3.另一种方式使用vs发布

很简单,下一步下一步做就好了。

八、通过Nginx部署到Linux服务器

1.在Centos7 上安装运行时

Register Microsoft key and feed

sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

Install the ASP.NET Core runtime

sudo yum install dotnet-sdk-3.1

2.安装libgdiplus

因为项目中使用验证码,需要用到这个命名空间:System.Drawing.Common
速度太慢,放弃。

3.将项目目录上传到linux

使用xshell 的ftp 轻松完成。

4.测试项目是否运行

cd /var/www/HotelWeb/ dotnet HotelWeb.dll [root@centos7 HotelWeb]# dotnet HotelWebMVC.dll info: Microsoft.Hosting.Lifetime[0] Now listening on: :5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production info: Microsoft.Hosting.Lifetime[0] Content root path: /var/www/HotelWeb

5.安装Nginx并配置

yum install nginx vim /etc/nginx/nginx.conf --------------- # 删除nginx默认的server,添加下面两个 server { listen 80 default_server; return 444; } server { listen 80; server_name *.hotel.com; location / { proxy_pass :5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } ----------------

6.启动nginx

systemctl start nginx nginx -t # 配置确认没有问题后,重新载入配置 nginx -s reload

7.浏览器测试

修改win7的host

192.168.30.110

8.最后的问题

因为libgdiplus这个库没有安装,所以进入验证码的页面会有报错。

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读