动态控制器服务是 Hoa Framework 一项非常强大的功能,大大提升了我们开发的速度和避免了很多配置WebAPI 的常见低级错误。既兼容了 ASP.NET Core 内置的 Web API模式 而且支持Swagger文档生成、多应用权限、文档分类等强大的功能,页提供了非常强大的拦截器操作。是目前构建 WebAPI首选方式。
using Hoa.Dependencies;
using Hoa.ServiceController.Attributes;
using System;
namespace Hoa.Application.Authorization
{
[HoaServiceController] // 贴了特性,动态控制器服务
public class TestAppService : ITestAppService, IAppServiceDependency // 继承了 IAppServiceDependency
{
public TestAppService()
{
}
[HttpGet]
public string GetName(){
return "Hoa";
}
[HttpPost]
public string Create(){
// TODO
}
[HttpPut]
public string Update(){
// TODO
}
[HttpDelete]
public string Delete(){
// TODO
}
[AcceptVerbs("GET","POST")]
pub string GetOrPost(){
// TODO
}
}
}
using Hoa.Dependencies;
using Hoa.ServiceController.Attributes;
using System;
namespace Hoa.Application.Authorization
{
[HoaServiceController] // 贴了特性,动态控制器服务
public class TestAppService : ITestAppService, IAppServiceDependency // 继承了 IAppServiceDependency
{
public TestAppService()
{
}
// 提示接已过时
[Obsolete("请调用/api/Test/GetWithPost接口")]
pub string GetOrPost(){
// TODO
}
}
}
using Hoa.Dependencies;
using Hoa.ServiceController.Attributes;
using System;
namespace Hoa.Application.Authorization
{
[HoaServiceController] // 贴了特性,动态控制器服务
public class TestAppService : ITestAppService, IAppServiceDependency // 继承了 IAppServiceDependency
{
public TestAppService()
{
}
[Route("/api/Groups/GetPost")]
pub string GetOrPost(){
// TODO
}
}
}
using FluentValidation;
using System.ComponentModel.DataAnnotations;
namespace Hoa.Application.Authorization.Dtos
{
public class SignInInput
{
[Required] // 必填
[EmailAddress] // 邮件地址
public string Email { get; set; }
[Required] // 必填
[MinLength(5)] // 最小长度
public string Password { get; set; }
[Required] // 必填
public UserType UserType { get; set; }
}
// FluentValidation 方式
public class SignInInputValidator : AbstractValidator<SignInInput>
{
public SignInInputValidator()
{
RuleFor(x => x.Email).Length(20, 250).WithMessage("长度在 20-250之间。");
}
}
}
生成多套分类文档
using Hoa.Application.Authorization.Dtos;
using Hoa.Dependencies;
using Hoa.ServiceController.Attributes;
using System;
namespace Hoa.Application.Authorization
{
[HoaServiceWebApiExplorer("AuthServer")]
[HoaServiceController]
public class AuthorizationAppService : IAuthorizationAppService, IAppServiceDependency
{
public AuthorizationAppService()
{
}
[HoaServiceWebApiExplorer("AuthServer")]
public SignInOutput SignIn(SignInInput input)
{
// TODO
}
// 会覆盖父类,而且会出现在 "Default" 分类中
[HoaServiceWebApiExplorer("Default")]
public SignInOutput DefaultSignIn(SignInInput input)
{
// TODO
}
}
}
using Hoa.Application.Authorization.Dtos;
using Hoa.Dependencies;
using Hoa.ServiceController.Attributes;
using System;
namespace Hoa.Application.Authorization
{
[HoaServiceWebApiExplorer("AuthServer")]
[HoaServiceController]
public class AuthorizationAppService : IAuthorizationAppService, IAppServiceDependency
{
public AuthorizationAppService()
{
}
[HoaServiceWebApiExplorer("AuthServer")]
public SignInOutput SignIn(SignInInput input)
{
// TODO
}
// 会出现在 "Default" 和 "AuthServer" 两个分类中,实现共享
[HoaServiceWebApiExplorer("Default","AuthServer")]
public SignInOutput DefaultSignIn(SignInInput input)
{
// TODO
}
}
}
生成同一个方法多个版本
using Hoa.Application.Authorization.Dtos;
using Hoa.Dependencies;
using Hoa.ServiceController.Attributes;
using System;
namespace Hoa.Application.Authorization
{
[HoaServiceWebApiExplorer("AuthServer")]
[HoaServiceController]
public class AuthorizationAppService : IAuthorizationAppService, IAppServiceDependency
{
public AuthorizationAppService()
{
}
public SignInOutput SignIn(SignInInput input)
{
// TODO
}
[HoaExportVersion("v2")]
public SignInOutput SignIn_v2(SignInInput input)
{
// TODO
}
}
}
支持多个请求方法访问同一个接口
[AcceptVerbs("GET")]
[AcceptVerbs("POST")]
强大的性能分析,SQL打印、异常输出
性能分析是 Hoa Framework 中 Swagger 文档一大特色,能够让我们看到实时的性能情况以及执行的数据库操作语句。是开发高质量的应用程序必备功能!
using Hoa.Application.Authorization.Dtos;
using Hoa.Dependencies;
using Hoa.ServiceController.Attributes;
using System;
namespace Hoa.Application.Authorization
{
[HoaServiceWebApiExplorer("AuthServer")]
[HoaServiceController]
public class AuthorizationAppService : IAuthorizationAppService, IAppServiceDependency
{
public AuthorizationAppService()
{
}
// 可匿名访问
[AllowAnonymous]
public SignInOutput SignIn(SignInInput input)
{
// TODO
}
// 必须授权才能访问
[Authorize]
public UserInfo GetUserInfo([Required] int userId)
{
// TODO
}
// 非常强大的多系统授权方式,支持多系统授权
[HoaMultipleClassifyAuthorize("A系统密钥","B系统密钥",...)]
public UserInfo GetUserInfo([Required] int userId)
{
// TODO
}
}
}