1 .创建WebAPI 支持docker ;
2 .创建DTO IService Service ,nuget 添加 panada.dynamicWebApi
3.在Api中配置Swagger,注意以下几点
(1) services.AddDynamicWebApi(); 添加动态WebApi 需放在 AddMvc 之后;
(2) Swagger 版本问题,我用的是3.1 所以我用的是swashbuckle 5.0.0-rc5(这块一定要注意,耽误好长时间);
(3) 配置注释xml
(4)代码配置
//注册Swagger生成器,定义一个和多个Swagger 文档
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1.1", new OpenApiInfo
{
Title = "My API",
Version = "v1.1",
Description = "API文档描述",
});
// (1)可以这样(动态)
c.DocInclusionPredicate((docName, description) => true);
foreach (var item in XmlCommentsFilePath)
{
c.IncludeXmlComments(item);
}
//(2) 也可以这样(静态)
//获取代码运行的相对路径
// var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
//插入代码上的注释放入Swagger
//c.IncludeXmlComments(Path.Combine(basePath, "Service.xml"));
//c.IncludeXmlComments(@"bin\Debug\netcoreapp3.1\webapitest1.xml");
//c.IncludeXmlComments(@"bin\Debug\netcoreapp3.1\Service.xml");
});
/**/
//启用Swagger
app.UseSwagger();
//配置Swagger UI
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1.1/swagger.json", "My API"); //注意中间段v1要和上面SwaggerDoc定义的名字保持一致
});
/**/
(5)可能出现的问题:
System.InvalidOperationException:“The action 'Service.AreaStatNumService.GetDataSource (Service)' has ApiExplorer enabled, but is using conventional routing. Only actions which use attribute routing support AiExplorper.”
解决: 这个就是版本问题了,因为.netcore 3.0 以后统一使用全局的OpenApi了
参考地址:(https://docs.microsoft.com/zh-cn/aspnet/core/web-api/microsoft.dotnet-openapi?view=aspnetcore-3.1 "使用 OpenAPI 工具开发 ASP.NET Core 应用")
4.配置全局标签
(1)配置异常标签,可以根据自己需要配合log使用
public class ApiExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
context.Result = BuildExceptionResult(context.Exception);
base.OnException(context);
}
///
/// 包装处理异常格式
///
///
///
private JsonResult BuildExceptionResult(Exception ex)
{
//应用程序业务级异常
//Logger.Error(ex.Message);
return new JsonResult(new ApiModelsBase()
{
code = ((int)HttpStatusCode.InternalServerError).ToString(),
msg = ex.Message
}
);
}
}
(2) 配置统一返回值格式标签
public class CustomActionFilter : ActionFilterAttribute, IActionFilter
{
public override void OnActionExecuting(ActionExecutingContext context)
{
//模型验证
if (!context.ModelState.IsValid)
{
throw new ApplicationException(context.ModelState.Values.First(p => p.Errors.Count > 0).Errors[0].ErrorMessage);
}
base.OnActionExecuting(context);
}
///
/// 处理正常返回的结果对象,进行统一json格式包装
/// 异常只能交由ExceptionFilterAttribute 去处理
///
///
public override void OnActionExecuted(ActionExecutedContext context)
{
if (context.Result != null)
{
var result = context.Result as ObjectResult;
JsonResult newresult;
if (context.Result is ObjectResult)
{
newresult = new JsonResult(new ApiModelsBase
{
code = ((int)HttpStatusCode.OK).ToString(),
msg = "",
data = result.Value
});
}
else if (context.Result is EmptyResult)
{
newresult = new JsonResult(new ApiModelsBase()
{
code = ((int)HttpStatusCode.OK).ToString()
,
msg = "",
data = new { }
});
}
else
{
throw new Exception($"未经处理的Result类型:{ context.Result.GetType().Name}");
}
context.Result = newresult;
}
base.OnActionExecuted(context);
}
}
也可以配置权限验证标签等,根据自己需求添加相应功能;
一个简单的小框架搭完了(记性不好,完全为了以后方便会议)。
ORM是自己写的,虽然BUG不少,逻辑问题也挺多,而且对数据库的兼容现在只有oracle he sqlserver ,慢慢拓展吧,就不往上show了。