启动项目,如图:
原来是英文的,我们要中文的,WeatherForecastController.cs是一个示例,删除即可,WeatherForecast.cs同时删除,当然不删除也行,这里是删除,创建自己的控制器
1创建model实体
namespace HouseSysWebApplication.Model
{///<summary>/// 图片实体类/// </summary>public class Book{/// <summary>/// 编号/// </summary>public int id { get; set; }/// <summary>/// 书名/// </summary>public string title { get; set; }/// <summary>/// 作者/// </summary>public string author { get; set; }/// <summary>/// 价格/// </summary>public float price { get; set; }/// <summary>/// 图片地址/// </summary>public string image { get; set; }}}
2 添加控制器BookController
Controller文件夹上右键,选择“添加”,选择“控制器”,选择API,
using HouseSysWebApplication.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;namespace HouseSysWebApplication.Controllers
{/// <summary>/// 图书控制器/// </summary>[Route("api/[controller]")][ApiController]public class BookController : ControllerBase{List<Book> bookList = InitBookStore();private static List<Book> InitBookStore(){List<Book> bookList = new List<Book>();Book b1 = new Book();b1.id = 101;b1.author = "杨康";b1.title = "Java从入门到精通";b1.price = 89.5f;b1.image = "img01.jpg";bookList.Add(b1);Book b2 = new Book();b2.id = 102;b2.author = "天涯";b2.title = "Java项目开发实战入门";b2.price = 49.5f;b2.image = "img02.jpg";bookList.Add(b2);Book b3 = new Book();b3.id = 103;b3.author = "标贵";b3.title = "Java编程思想";b3.price = 76f;b3.image = "img03.jpg";bookList.Add(b3);Book b4 = new Book();b4.id = 104;b4.author = "西法";b4.title = "web开发手册";b4.price = 78.3f;b4.image = "img04.jpg";bookList.Add(b4);Book b5 = new Book();b5.id = 105;b5.author = "海风";b5.title = "唐诗宋词";b5.price = 29.5f;b5.image = "img05.jpg";bookList.Add(b5);return bookList;}/// <summary>/// 获取所有图书/// </summary>/// <returns>图书列表</returns>[HttpGet("all")]public List<Book> BookStore(){return bookList;}/// <summary>/// 查询指定名称的图书/// </summary>/// <param name="bookName">图书标题</param>/// <returns>图书列表</returns>[HttpGet("search")]public IEnumerable<Book> BookStore(string bookName){return bookList.FindAll(x => x.title.Contains(bookName)).AsEnumerable();}}
}
这里要注意的是代码的完整注释要写上,既然是写接口,生成的文档所以要详细,可读性强,启动后效果
3 生成xml文档
项目右键,选择属性,点击生成,勾选
将项目编译生成,就会自动创建xml文档
<?xml version="1.0"?>
<doc><assembly><name>HouseSysWebApplication</name></assembly><members><member name="T:HouseSysWebApplication.Controllers.BookController"><summary>图书控制器</summary></member><member name="M:HouseSysWebApplication.Controllers.BookController.BookStore"><summary>获取所有图书</summary><returns>图书列表</returns></member><member name="M:HouseSysWebApplication.Controllers.BookController.BookStore(System.String)"><summary>查询指定名称的图书</summary><param name="bookName">图书标题</param><returns>图书列表</returns></member><member name="T:HouseSysWebApplication.Model.Book"><summary>图片实体类</summary></member><member name="P:HouseSysWebApplication.Model.Book.id"><summary>编号</summary></member><member name="P:HouseSysWebApplication.Model.Book.title"><summary>书名</summary></member><member name="P:HouseSysWebApplication.Model.Book.author"><summary>作者</summary></member><member name="P:HouseSysWebApplication.Model.Book.price"><summary>价格</summary></member><member name="P:HouseSysWebApplication.Model.Book.image"><summary>图片地址</summary></member></members>
</doc>
4修改Program.cs
只要改一个地方就行
builder.Services.AddSwaggerGen(c =>
{c.SwaggerDoc("v1", new OpenApiInfo { Title = "微信小程序后端api文档", Version = "v1" });// var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)// var xmlPath = Path.Combine(basePath, "HouseSysWebApplication.xml");var xmlPath = Path.Combine(AppContext.BaseDirectory, "HouseSysWebApplication.xml");c.IncludeXmlComments(xmlPath, true);//true:显示控制器注释c.OrderActionsBy(o => o.RelativePath);//action名称排序
});
5编译生成,启动,看效果,立马中文
选择某个接口,比如第1个
,点击测试,没有问题,说明一切杠杠的
注释没有问题,相当的帅,单独访问也正常
6、IIS操作
7、修改web.config文件
原来是这样的
<?xml version="1.0" encoding="utf-8"?>
<configuration><location path="." inheritInChildApplications="false"><system.webServer><handlers><add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /></handlers><aspNetCore processPath="dotnet" arguments=".\HouseSysWebApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" /></system.webServer></location>
</configuration>
<!--ProjectGuid: b0eb1c5f-e8f5-4e0e-9968-c58f6c4d78db-->
增加一个内容
完整的内容是这样的
<?xml version="1.0" encoding="utf-8"?>
<configuration><location path="." inheritInChildApplications="false"><system.webServer><handlers><add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /></handlers><aspNetCore processPath="dotnet" arguments=".\HouseSysWebApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" ><environmentVariables><environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /></environmentVariables></aspNetCore></system.webServer></location><system.webServer><defaultDocument><files><clear /><add value="index.htm" /><add value="Default.htm" /><add value="Default.asp" /><add value="index.html" /><add value="iisstart.htm" /></files></defaultDocument></system.webServer>
</configuration>
<!--ProjectGuid: b0eb1c5f-e8f5-4e0e-9968-c58f6c4d78db-->
8、修改网站默认首页
NetCore6.0 WebApi 发布IIS服务器后网站无法打开报404问题处理
情况介绍:
新建了个.NetCore 5.0 的 WebAPI项目,发布部署到WINDOWS10 的 IIS后,默认打开 http://127.0.0.1/swagger 出现404找不到页面的错误。
原因:
因为.NetCore 5.0 自带集成了Swagger , 在发布项目时,通常会在代码中限制ASPNETCORE_ENVIRONMENT为Production时关闭Swagger。
解决方法:
项目发布后默认为 Production 生产环境,可修改 web.config 文件,改成 Development 开发环境就可以访问。
具体方法如下:在web.config中添加一下代码
<environmentVariables><environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /></environmentVariables>
C#编写ASP.NET Core的Web API并部署到IIS的部署方法及常见问题,原文链接
C#编写ASP.NET Core的Web API并部署到IIS上的详细教程(API用于准确获取Word/Excel/PPT/PDF的页数)6 -将项目部署到IIS,及常见错误解决方案_.net core 动态生成 word 接口-CSDN博客
6.2 网页报错“HTTP 错误 500.19 - Internal Server Error ”
IIS网站创建好后,点击浏览网站,浏览器页面报错“HTTP 错误 500.19 - Internal Server Error ”。
这个问题的原因是当前运行环境没有安装AspNetCoreModule。
请参考本文:
“4.4 安装AspNetCoreModule(Win10/Server通用)”这小节的操作步骤。