Skip to content

Commit 64f44af

Browse files
committed
Update to p25
1 parent 098134c commit 64f44af

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ __ASP.NET Core 3.0 实现 RESTful API 的学习笔记__
44
<br><br>
55
跟随杨旭老师(solenovex)的[博客](https://www.cnblogs.com/cgzl/p/11814971.html)[视频](https://www.bilibili.com/video/av77957694?from=search&seid=17664776753878261104)课程,学习 RESTful API 在ASP.NET Core 3.0 上的实现。
66
<br>
7-
包含课程中搭建的项目与部分笔记,目前覆盖到视频 P24
7+
包含课程中搭建的项目与部分笔记,目前覆盖到视频 P25
88
<br><br><br>
99
非常感谢杨老师 🤗
1010
<br>

Routine/Routine.APi/Controllers/CompaniesController.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* PATCH - 局部修改/更新 | N N
1818
* PUT - 如果存在就替换,不存在则创建 | N Y
1919
* DELETE - 移除/删除 | N Y
20-
* OPTIONS - | Y Y
21-
* HEAD - | Y Y
20+
* OPTIONS - 获取 Web API 的通信选项的信息 | Y Y
21+
* HEAD - 只请求页面的首部 | Y Y
2222
*
2323
* 安全性是指方法执行后并不会改变资源的表述
2424
* 幂等性是指方法无论执行多少次都会得到同样的结果
@@ -136,5 +136,12 @@ public async Task<IActionResult> CreateCompany([FromBody]CompanyAddDto company)
136136
//通过使用 CreatedAtRoute 返回时可以在 Header 中添加一个地址(Loaction)
137137
return CreatedAtRoute(nameof(GetCompany), new { companyId = returnDto.Id }, returnDto);
138138
}
139+
140+
[HttpOptions]
141+
public IActionResult GetCompaniesOptions()
142+
{
143+
Response.Headers.Add("Allow", "GET,POST,OPTIONS");
144+
return Ok();
145+
}
139146
}
140147
}

Routine/Routine.APi/Startup.cs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,38 @@ public Startup(IConfiguration configuration)
3030
// 注册服务 This method gets called by the runtime. Use this method to add services to the container.
3131
public void ConfigureServices(IServiceCollection services)
3232
{
33+
/*
34+
* 内容协商:
35+
* 针对一个响应,当有多种表述格式的时候,选取最佳的一个表述,例如 application/json、application/xml
36+
*
37+
* Accept Header 指明服务器输出格式,对应 ASP.NET Core 里的 Output Formatters
38+
* 如果服务器不支持客户端请求的媒体类型(Media Type),返回状态码406
39+
*
40+
* Content-Type Header 指明服务器输入格式,对应 ASP.NET Core 里的 Input Formatters
41+
*/
42+
43+
//以下是一种较旧的写法,在本项目中不使用
44+
//services.AddControllers(options =>
45+
//{
46+
// //启用406状态码
47+
// options.ReturnHttpNotAcceptable = true;
48+
49+
// //OutputFormatters 默认有且只有 Json 格式
50+
// //添加对输出 XML 格式的支持
51+
// //此时默认输出格式依然是 Json ,因为 Json 格式位于第一位置
52+
// options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
53+
54+
// //如果在 Index 0 位置插入对 XML 格式的支持,那么默认输出格式是 XML
55+
// //options.OutputFormatters.Insert(0, new XmlDataContractSerializerOutputFormatter());
56+
//});
57+
//
58+
//以下是较新的写法,使用诸如 AddXmlDataContractSerializerFormatters() 等,使用更方便。
3359
services.AddControllers(options =>
3460
{
35-
/*
36-
* 内容协商:
37-
* 针对一个响应,当有多种表述格式的时候,选取最佳的一个表述,例如 application/json、application/xml
38-
*
39-
* Accept Header 指明服务器输出格式,对应 ASP.NET Core 里的 Output Formatters
40-
* 如果服务器不支持客户端请求的媒体类型(Media Type),返回状态码406
41-
*
42-
* Content-Type Header 指明服务器输入格式,对应 ASP.NET Core 里的 Input Formatters
43-
*/
44-
4561
//启用406状态码
4662
options.ReturnHttpNotAcceptable = true;
4763

48-
//OutputFormatters 默认有且只有 Json 格式
49-
//添加对 XML 格式的支持
50-
//此时默认格式依然是 Json ,因为 Json 格式位于第一位置
51-
options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
52-
53-
//如果在 Index 0 位置插入对 XML 格式的支持,那么默认格式是 XML
54-
//options.OutputFormatters.Insert(0, new XmlDataContractSerializerOutputFormatter());
55-
});
56-
57-
//以下是另一种较新的写法,同样实现对 XML 格式的支持
58-
//services.AddControllers(options =>
59-
//{
60-
// options.ReturnHttpNotAcceptable = true;
61-
//}).AddXmlDataContractSerializerFormatters();
64+
}).AddXmlDataContractSerializerFormatters(); //添加对 XML 输入与输出格式的支持
6265

6366
//使用 AutoMapper,扫描当前应用域的所有 Assemblies 寻找 AutoMapper 的配置文件
6467
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

0 commit comments

Comments
 (0)