# Panda.DynamicWebApi
Language: English | [中文](README_zh-CN.md)
`Panda.DynamicWebApi` is a component that dynamically generates WebApi. The generated API conforms to the Restful style and is inspired by ABP. It can generate WebApi based on qualified classes. The logic is directly called by the MVC framework. There is no performance problem. It is perfectly compatible with Swagger to build the API documentation. There is no difference compared with manually writing the Controller.
Application Scenario: The application logic layer in the DDD architecture, which can be used to directly generate WebApi without using the Controller.
[![Latest version](https://img.shields.io/nuget/v/Panda.DynamicWebApi.svg)](https://www.nuget.org/packages/Panda.DynamicWebApi/)
## 1.Quick Start
(1)Create a new ASP.NET Core WebApi (or MVC) project
(2)Install Package
````shell
Install-Package Panda.DynamicWebApi
````
(3)Create a class named `AppleAppService`, implement the `IDynamicWebApi` interface, and add the attribute `[DynamicWebApi]`
````csharp
[DynamicWebApi]
public class AppleAppService: IDynamicWebApi
{
private static readonly Dictionary<int,string> Apples=new Dictionary<int, string>()
{
[1]="Big Apple",
[2]="Small Apple"
};
/// <summary>
/// Get An Apple.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id:int}")]
public string Get(int id)
{
if (Apples.ContainsKey(id))
{
return Apples[id];
}
else
{
return "No Apple!";
}
}
/// <summary>
/// Get All Apple.
/// </summary>
/// <returns></returns>
public IEnumerable<string> Get()
{
return Apples.Values;
}
public void Update(UpdateAppleDto dto)
{
if (Apples.ContainsKey(dto.Id))
{
Apples[dto.Id] =dto.Name;
}
}
/// <summary>
/// Delete Apple
/// </summary>
/// <param name="id">Apple Id</param>
[HttpDelete("{id:int}")]
public void Delete(int id)
{
if (Apples.ContainsKey(id))
{
Apples.Remove(id);
}
}
}
````
(4)Register DynamicWebApi in Startup
````csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddDynamicWebApi();
}
````
(5)Add Swagger
(6)Run
After running the browser, visit `<your project address>/swagger/index.html` and you will see the WebApi generated for our `AppleAppService`.
![1560265120580](assets/1560265120580.png)
This quick start Demo Address: [link](/samples/Panda.DynamicWebApiSample)
## 2.Advanced
(1)There are two conditions that need to be met for a class to generate a dynamic API. One is that the class **direct** or **indirect** implements `IDynamicWebApi`, and the class **itself** or **parent abstract class** or **Implemented interface** has the property `DynamicWebApi`
(2)Adding the attribute `[NonDynamicWebApi]` allows a class or a method to not generate an API, and `[NonDynamicWebApi]` has the highest priority.
(3)The suffix of the dynamic API **class name** that conforms to the rule is deleted. For example, our quick start `AppleAppService` will delete the AppService suffix. This rule can be dynamically configured.
(4)The API route prefix is automatically added, and the `api` prefix is added by default for all APIs.
(5)The default HTTP verb is `POST`, which can be understood as the Http Method of the API. But it can be overridden by `HttpGet/HttpPost/HttpDelete ` and other ASP.NET Core built-in attribute.
(6)You can override the default route with built-in attribute such as `HttpGet/HttpPost/HttpDelete `
(7)By default, HTTP verbs are set according to your method name. For example, the API verb generated by CreateApple or Create is `POST`. The comparison table is as follows. If you hit (ignore uppercase) the comparison table, then this part of the API name will be Omitted, such as CreateApple will become Apple, if not in the following comparison table, will use the default verb `POST`
| MethodName Start With | Http Verb |
| --------------------- | --------- |
| create | POST |
| add | POST |
| post | POST |
| get | GET |
| find | GET |
| fetch | GET |
| query | GET |
| update | PUT |
| put | PUT |
| delete | DELETE |
| remove | DELETE |
(8)It is highly recommended that the method name use the PascalCase specification and use the verbs from the above table. Such as:
Create Apple Info-> Add/AddApple/Create/CreateApple
Update Apple Info -> Update/UpdateApple
...
(9)The `[DynamicWebApi]` attribute can be inherited, so it is forbidden to be placed on a parent class other than an abstract class or interface for the parent class to be misidentified.
## 3.Configuration
All configurations are in the object `DynamicWebApiOptions`, as explained below:
| Property | Require | 说明 |
| --------------------------- | -------- | --------------------------------------------------------- |
| DefaultHttpVerb | False | Default:POST. |
| DefaultAreaName | False | Default:Empty. |
| DefaultApiPrefix | False | Default:api. Web API route prefix. |
| RemoveControllerPostfixes | False | Default:AppService/ApplicationService. The suffix of the class name that needs to be removed. |
| RemoveActionPostfixes | False | Default:Async. Method name needs to be removed from the suffix. |
| FormBodyBindingIgnoredTypes | False | Default:IFormFile。Ignore type for MVC Form Binding. |
## 4.Q&A
If you encounter problems, you can use [Issues](https://github.com/dotnetauth/Panda.DynamicWebApi/issues) to ask questions.
## 5.Reference project
> This project directly or indirectly refers to the following items
- [ABP](https://github.com/aspnetboilerplate/aspnetboilerplate)
没有合适的资源?快使用搜索试试~ 我知道了~
dotnet-PandaDynamicWebApi是一个动态生成WebApi的组件生成的API符合Restful风格
共46个文件
cs:27个
json:6个
csproj:4个
需积分: 21 1 下载量 175 浏览量
2019-08-15
00:57:02
上传
评论
收藏 101KB ZIP 举报
温馨提示
Panda.DynamicWebApi 是一个动态生成WebApi的组件,生成的API符合Restful风格,受启发于ABP。它可以根据符合条件的类来生成WebApi,由MVC框架直接调用逻辑,无性能问题,完美兼容Swagger来构建API说明文档,与手动编写Controller相比并无区别。
资源详情
资源评论
资源推荐
收起资源包目录
dotnet-PandaDynamicWebApi是一个动态生成WebApi的组件生成的API符合Restful风格.zip (46个子文件)
Panda.DynamicWebApi-master
.gitignore 5KB
src
Panda.DynamicWebApi
AppConsts.cs 1KB
IDynamicWebApi.cs 93B
DynamicWebApiControllerFeatureProvider.cs 1020B
Attributes
NonDynamicWebApiAttribute.cs 255B
DynamicWebApiAttribute.cs 2KB
DynamicWebApiConvention.cs 10KB
DynamicWebApiServiceExtensions.cs 2KB
Panda.DynamicWebApi.csproj 1KB
Helpers
ServiceCollectionExtensions.cs 3KB
ExtensionMethods.cs 4KB
ReflectionHelper.cs 3KB
TypeHelper.cs 2KB
DynamicWebApiOptions.cs 3KB
practice
XiaoChen.StudentManagement
XiaoChen.StudentManagement.sln 3KB
src
Xc.StuMgr.Application
Dtos
StudentOutput.cs 204B
CreateStudentInput.cs 102B
UpdateStudentInput.cs 208B
StudentAppService.cs 2KB
IStudentAppService.cs 1KB
IApplicationService.cs 207B
Xc.StuMgr.Application.csproj 497B
Xc.StuMgr.WebHost
Program.cs 426B
appsettings.Development.json 137B
Controllers
ValuesController.cs 923B
appsettings.json 97B
Xc.StuMgr.WebHost.csproj 933B
Startup.cs 2KB
Properties
launchSettings.json 748B
assets
1560265120580.png 60KB
Alipay.AopSdk.AspnetCore.svg 952B
0F703DDD.png 3KB
samples
Panda.DynamicWebApiSample
Program.cs 623B
appsettings.Development.json 137B
Dtos
UpdateAppleDto.cs 170B
Controllers
ValuesController.cs 1KB
appsettings.json 97B
Panda.DynamicWebApiSample.csproj 830B
Startup.cs 2KB
Dynamic
AppleAppService.cs 2KB
IApplicationService.cs 213B
Properties
launchSettings.json 785B
LICENSE 11KB
README.md 6KB
Panda.DynamicWebApi.sln 2KB
README_zh-CN.md 6KB
共 46 条
- 1
weixin_39840914
- 粉丝: 436
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 的玩具 Python 实现.zip
- RHCE linux下的火墙管理 及下载
- ESP32-C3FH4 : UltraLowPower SoC with RISCV SingleCore CPU Supporting 2.4 GHz WiFi and Bluetooth LE
- 用于解包和反编译由 Python 代码编译的 EXE 的辅助脚本 .zip
- 用于自动执行任务的精选 Python 脚本列表.zip
- 全国IT学科竞赛蓝桥杯的比赛特点及参赛心得
- 用于编码面试审查的算法和数据结构 .zip
- 用于操作 ESC,POS 打印机的 Python 库.zip
- 用于控制“Universal Robots”机器人的 Python 库.zip
- 用于控制 Broadlink RM2,3 (Pro) 遥控器、A1 传感器平台和 SP2,3 智能插头的 Python 模块.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0