Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_dotnet
提交
e68968c4
S
skill_tree_dotnet
项目概览
CSDN 技术社区
/
skill_tree_dotnet
通知
30
Star
6
Fork
4
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_dotnet
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e68968c4
编写于
12月 20, 2021
作者:
Admini$trat0r
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
添加 2..NET中阶\1.ASP.NET Core应用\4.Web API应用
上级
ce3bf968
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
237 addition
and
0 deletion
+237
-0
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/WebAPI.json
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/WebAPI.json
+8
-0
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/WebAPI.md
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/WebAPI.md
+136
-0
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/sample/Program.cs
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/sample/Program.cs
+83
-0
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/sample/sample.csproj
.....NET中阶/1.ASP.NET Core应用/4.Web API应用/sample/sample.csproj
+10
-0
未找到文件。
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/WebAPI.json
0 → 100644
浏览文件 @
e68968c4
{
"type"
:
"code_options"
,
"author"
:
"Gao996"
,
"source"
:
"WebAPI.md"
,
"exercise_id"
:
"7387559d89a94c03a5dc8a963b0327a0"
,
"notebook_enable"
:
false
}
\ No newline at end of file
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/WebAPI.md
0 → 100644
浏览文件 @
e68968c4
# 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
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/sample/Program.cs
0 → 100644
浏览文件 @
e68968c4
// 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
data/2..NET中阶/1.ASP.NET Core应用/4.Web API应用/sample/sample.csproj
0 → 100644
浏览文件 @
e68968c4
<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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录