日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

NSwagStudio for Swagger Api

發(fā)布時間:2023/11/27 生活经验 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NSwagStudio for Swagger Api 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本案例主要說明如何使用NSwag 工具使用桌面工具快速生成c# 客戶端代碼、快速的訪問Web Api。

NSwagStudio 下載地址?比較強大、可以生成TypeScript、WebApi Controller、CSharp Client?

1、運行WebApi項目??URL?http://yourserver/swagger 然后你將看到界面如下

? ? 1.1 Web API 代碼

? ? 該代碼使用的是Abp框架、如果不了解Abp框架的請到官網?http://www.aspnetboilerplate.com/?了解

  • ?實休代碼
       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 應用
     [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; }}
    Dtos
      public 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));}}
    Application Service

? 2、生成客戶端代碼 并且訪問Web API

? ?2.1 安裝工具和創(chuàng)建測試客戶端項目

  • 安裝Install NSwagStudio
  • 創(chuàng)建一個新的c#客戶端項目
  • 將所需的程序集依賴項添加到庫項目中

? ?2.2 生成代碼

  • 啟動 NSwagStudio 然后選擇 Swagger Specification
  • 在Load Swagger Specification from URL:?http://yourserver/swagger/v1/swagger.json ?前期條件是服務必須期啟動
  • 選擇一個右邊的選項卡 如:"CSharpClient"、然后點擊“?Generate Outputs”
  • 復制生所的代碼到你的客戶端項目中
  • 同時也可以設置、如項目命名空間、以及配置輸出文件路經、生成DTO的一些配置

? ? ?

? 3、保存腳本

  • 保存文件.nswag 把當前的一些配置保存

? ? ? ????

?4、 客戶端代碼實現(xiàn)

 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                 //新增數(shù)據(jù)
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;

?

轉載于:https://www.cnblogs.com/w2011/p/5979708.html

總結

以上是生活随笔為你收集整理的NSwagStudio for Swagger Api的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。