提交 e68968c4 编写于 作者: Admini$trat0r's avatar Admini$trat0r

添加 2..NET中阶\1.ASP.NET Core应用\4.Web API应用

上级 ce3bf968
{
"type": "code_options",
"author": "Gao996",
"source": "WebAPI.md",
"exercise_id": "7387559d89a94c03a5dc8a963b0327a0",
"notebook_enable": false
}
\ No newline at end of file
# Web API应用
应用程序接口(API,Application Programming Interface)是基于编程语言构建的结构,使开发人员更容易地创建复杂的功能。它们抽象了复杂的代码,并提供一些简单的接口规则直接使用。
当我们创建一个.net Core的Web API时,可以选择带有模板的,这样就自动生成了一个简单的API,而且调试的时候也会自动生成[swagger](https://swagger.io/ "swagger")打开网页可查看接口详细信息和测试。如下是ValueController的代码。
```csharp
[Route("api/[controller]")] // 自定义特性,调用接口时可以"https://localhost/api/Values"(这里[controller]是继承ControllerBase的类名,也可以拼接很多,比如加[action]是方法名)
[ApiController]
public class ValuesController : ControllerBase
{
// GET: api/<ValuesController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
```
此时用GET调用接口会返回
```json
[
"value1",
"value2"
]
```
Web API还允许在方法中使用特性来切换请求方式和参数方式:
```csharp
// GET api/<ValuesController>/5
[HttpGet("{id}")] // 这里的{id}就是参数id
public string Get(int id)
{
return (id * 2).ToString();
}
// POST api/<ValuesController>
[HttpPost] // 这里用了POST方法
public void Post([FromBody] string value) //FromBody特性,curl -X POST "https://localhost/api/Values" -H "accept: */*" -H "Content-Type: application/json" -d "\"string\""
{
}
// PUT api/<ValuesController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<ValuesController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
```
```csharp
[Route("api/[controller]/[action]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpGet]
public string Sum(string num1, string num2)
{
int num1Length = num1.Length;
int num2Length = num2.Length;
int maxLength = Math.Max(num1Length, num2Length);
num1 = num1.PadLeft(maxLength, '0');
num2 = num2.PadLeft(maxLength, '0');
int sum = 0, ans = 0;
string result = string.Empty;
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= maxLength; i++)
{
sum = num1[maxLength - i] - '0' + num2[maxLength - i] - '0' + ans;
if (sum >= 10)
{
sum = sum % 10;
ans = 1;
}
else
{
ans = 0;
}
sb.Append(sum);
}
if (ans == 1)
{
sb.Append(1);
}
result = string.Join("", sb.ToString().Reverse());
return result;
}
[HttpGet("{str}")]
public string Copy(string str)
{
return str;
}
}
```
如上代码,在下列选型中,可以正确获取Test接口的是:
## 答案
```
curl -X GET "https://localhost:44326/api/Test/Sum?num1=6&num2=6" -H "accept: text/plain"
curl -X GET "https://localhost:44326/api/Test/Copy/str" -H "accept: text/plain"
```
## 选项
### A
```
curl -X GET "https://localhost:44326/api/Test/Sum/6&6" -H "accept: text/plain"
curl -X GET "https://localhost:44326/api/Test/Copy/str" -H "accept: text/plain"
```
### B
```
curl -X GET "https://localhost:44326/api/Test/Sum?num1=6&num2=6" -H "accept: text/plain"
curl -X GET "https://localhost:44326/api/Test/Copy?str=str" -H "accept: text/plain"
```
### C
```
curl -X GET "https://localhost:44326/api/Test/Sum/6&6" -H "accept: text/plain"
curl -X GET "https://localhost:44326/api/Test/Copy?str=str" -H "accept: text/plain"
```
\ No newline at end of file
// Test API
[Route("api/[controller]/[action]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpGet]
public string Sum(string num1, string num2)
{
int num1Length = num1.Length;
int num2Length = num2.Length;
int maxLength = Math.Max(num1Length, num2Length);
num1 = num1.PadLeft(maxLength, '0');
num2 = num2.PadLeft(maxLength, '0');
int sum = 0, ans = 0;
string result = string.Empty;
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= maxLength; i++)
{
sum = num1[maxLength - i] - '0' + num2[maxLength - i] - '0' + ans;
if (sum >= 10)
{
sum = sum % 10;
ans = 1;
}
else
{
ans = 0;
}
sb.Append(sum);
}
if (ans == 1)
{
sb.Append(1);
}
result = string.Join("", sb.ToString().Reverse());
return result;
}
[HttpGet("{str}")]
public string Copy(string str)
{
return str;
}
}
// Value API
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET: api/<ValuesController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<ValuesController>/5
[HttpGet("{id}")]
public string Get(int id)
{
return (id * 2).ToString();
}
// POST api/<ValuesController>
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/<ValuesController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<ValuesController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册