Files
Yi.Framework/Yi.Doc.Md/02.框架功能模块/02.动态Api.md
2023-12-15 23:44:35 +08:00

61 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 简介
控制器层通常不包含业务的,我们控制器的代码经常是如下:
``` cs
[HttpGet]
[route("Info")]
pulic IActionResult GetInfo()
{
retrun Ok(_service.GetInfo());
}
```
我们不仅要创建控制器文件,还要写出应用层到控制器的方法,将业务的数据通过控制器暴露出去
> 控制器只做转发,没有做任何事情,形成了大量的冗余代码
## 如何使用
> 推荐直接在应用层中直接使用
使用动态Api需要3个条件
1. 任何一个类,实现`IRemoteService`接口
2. 该类需要加入DI容器
3. 在管道模型中配置动态Api服务:
> 通常我们直接继承`ApplicationService`即可,因为该类实现了`IRemoteService`
``` cs
//动态Api
Configure<AbpAspNetCoreMvcOptions>(options =>
{
options.ConventionalControllers.Create(typeof(YiAbpApplicationModule).Assembly, options => options.RemoteServiceName = "default");
});
```
根据方法名自动映射Http方法及路由
例如:
- GetInfoGet请求
- UpdateInfoPut请求
- RemoveInfo: Del请求
- CreateInfo: Post请求
## 完整例子
``` cs
using Volo.Abp.Application.Services;
namespace Yi.Abp.Application.Services
{
public class TestService : ApplicationService
{
/// <summary>
/// 你好世界
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetHelloWorld(string? name)
{
return name ?? "HelloWord";
}
}
}
```