启动项目,如图:
原来是英文的,我们要中文的,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-->