本案例主要說明如何使用NSwag 工具使用桌面工具快速生成c# 用戶端代碼、快速的通路Web Api。
NSwagStudio 下載下傳位址 比較強大、可以生成TypeScript、WebApi Controller、CSharp Client
1、運作WebApi項目 URL http://yourserver/swagger 然後你将看到界面如下
http://yourserver/swagger 然後你将看到界面如下

1.1 Web API 代碼
該代碼使用的是Abp架構、如果不了解Abp架構的請到官網 http://www.aspnetboilerplate.com/ 了解
- 實休代碼
NSwagStudio for Swagger Api NSwagStudio for Swagger Api
實體代碼public class UserInformation : Entity<int>, IHasCreationTime, IHasModificationTime { [StringLength(20)] public string UserName { get; set; } public int UserAge { get; set; } [StringLength(20)] public string UserAddress { get; set; } public UserInformation() { CreationTime = Clock.Now; } public DateTime CreationTime { get; set; } public DateTime? LastModificationTime { get; set; } }
- Application 應用
NSwagStudio for Swagger Api NSwagStudio for Swagger Api
Dtos[AutoMapTo(typeof(UserInformation))] public class CreateUserInformationDto : IHasCreationTime { public string UserName { get; set; } public int UserAge { get; set; } public string UserAddress { get; set; } public DateTime CreationTime { get; set; } public CreateUserInformationDto() { CreationTime = Clock.Now; } } public class DeleteUserInformationDto : IEntityDto { public int Id { get; set; } } public class GetAllUserInformationDto : PagedAndSortedResultRequestDto, ICustomValidate { public string UserName { get; set; } public void AddValidationErrors(CustomValidationContext context) { if (string.IsNullOrEmpty(UserName)) { context.Results.Add(new ValidationResult("使用者名稱關鍵字不能為空!")); } } } public class GetInputUserInformatinDto : IEntityDto { public int Id { get; set; } } [AutoMapTo(typeof(UserInformation))] public class UpdateUserInformationDto : CreateUserInformationDto, IEntityDto, IHasModificationTime { public UpdateUserInformationDto() { LastModificationTime = Clock.Now; } public DateTime? LastModificationTime { get; set; } public int Id { get; set; } } public class UserInformationDto : EntityDto, IHasCreationTime, IHasModificationTime { public string UserName { get; set; } public int UserAge { get; set; } public string UserAddress { get; set; } public UserInformationDto() { CreationTime = Clock.Now; } public DateTime CreationTime { get; set; } public DateTime? LastModificationTime { get; set; } }
NSwagStudio for Swagger Api NSwagStudio for Swagger Api
Application Servicepublic interface IUserAppService : ICrudAppService<UserInformationDto, int, GetAllUserInformationDto, CreateUserInformationDto, UpdateUserInformationDto, GetInputUserInformatinDto, DeleteUserInformationDto> { } public class UserAppService : CrudAppService <UserInformation, UserInformationDto, int, GetAllUserInformationDto, CreateUserInformationDto, UpdateUserInformationDto, GetInputUserInformatinDto, DeleteUserInformationDto>, IUserAppService { private ICacheManager cacheManager; public UserAppService(IRepository<UserInformation, int> repository,ICacheManager cache) : base(repository) { cacheManager = cache; } protected override IQueryable<UserInformation> CreateFilteredQuery(GetAllUserInformationDto input) { return base.CreateFilteredQuery(input).Where(o=>o.UserName.Contains(input.UserName)); } }
2、生成用戶端代碼 并且通路Web API
2.1 安裝工具和建立測試用戶端項目
- 安裝Install NSwagStudio
- 建立一個新的c#用戶端項目
- 将所需的程式集依賴項添加到庫項目中
2.2 生成代碼
- 啟動 NSwagStudio 然後選擇 Swagger Specification
- 在Load Swagger Specification from URL:
http://yourserver/swagger/v1/swagger.json 前期條件是服務必須期啟動
- 選擇一個右邊的頁籤 如:"CSharpClient"、然後點選“ Generate Outputs”
- 複制生所的代碼到你的用戶端項目中
- 同時也可以設定、如項目命名空間、以及配置輸出檔案路經、生成DTO的一些配置
3、儲存腳本
- 儲存檔案.nswag 把目前的一些配置儲存
4、 用戶端代碼實作
2 UserClient app = new UserClient();
3 var data = app.GetAllAsync(new GetAllUserInformationDto()
4 {
5 MaxResultCount = 1,
6 SkipCount = 1,
7 Sorting = "desc",
8 UserName = "luyong"
9 });
10
11 //新增資料
12 var app2= app.CreateAsync(new CreateUserInformationDto()
13 {
14 CreationTime = DateTime.Now,
15 UserAddress = "china",
16 UserName = "fadf333",
17 UserAge = 10
18 });
19
20 dataGridView1.DataSource = data.Result.Items;