当前位置:首页 » 《关于电脑》 » 正文

(四)webAPI的发布和访问

0 人参与  2024年10月07日 13:21  分类 : 《关于电脑》  评论

点击全文阅读


我们已经创建了一个core webapi项目,基于.net6.0,默认包含WeatherForecastController控制器。(可参见前几期的博文)。

1.项目发布

使用命令 dotnet publish -o publish来发布项目。(也可以右击项目->发布->文件夹,来进行发布)

发布后,在项目根目录下创建了“publish”文件夹。

2.项目启动

打开publish文件夹,双击CoreWebAPI.exe启动项目。

http默认访问地址:http://localhost:5000/WeatherForecast

https默认访问地址:https://localhost:5001/WeatherForecast

3.更改发布地址

更改appsettings.json文件,添加Kestrel,我们设置http为5003端口。重新生成、发布。

{  "Logging": {    "LogLevel": {      "Default": "Information",      "Microsoft.AspNetCore": "Warning"    }  },  "AllowedHosts": "*",  "Kestrel": {    "Endpoints": {       "Http": {         "Url": "http://localhost:5003"       }     }   }}

 http访问地址:http://localhost:5003/WeatherForecast

 

4.运行环境,采用swagger访问

更改appsettings.json文件,添加配置。

  "AppSettings": {    "Swagger": {      "Enabled": true    }  }

在Program.cs文件(或者StartUp.cs文件),通过配置判断是否启用Swagger方式。重新生成、发布

var swaggerEnabled = app.Configuration.GetValue<bool>("AppSettings:Swagger:Enabled");if (swaggerEnabled){    app.UseSwagger();    app.UseSwaggerUI();}

采用 swagger地址访问。为了安全起见,一般会把swagger方式关闭。

设置swagger访问的标题:

builder.Services.AddSwaggerGen(c =>c.SwaggerDoc("v1", new OpenApiInfo { Title = "我的webAPI项目", Version = "v1" }));

重新生成、发布:


点击全文阅读


本文链接:http://zhangshiyu.com/post/168540.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1