AutomaticApi can automatically generate APIs based on services, just define a REST API interface.
Package | NuGet Stable | Downloads |
---|---|---|
AutomaticApi | ||
AutomaticApi.Abstraction |
public class DemoService
{
public string Get()
{
return "Hello AutomaticApi";
}
}
public interface IDemoAService : IAutomaticApi
{
/// <summary>
/// DemoA api
/// </summary>
/// <returns></returns>
string Get();
}
We can also define another REST API interface.
public interface IDemoBService : IAutomaticApi
{
/// <summary>
/// DemoB api
/// </summary>
/// <returns></returns>
string Get();
}
And implement these interfaces.
public class DemoService : IDemoAService, IDemoBService
...
public void ConfigureServices(IServiceCollection services)
{
services.AddAutomaticApi(op =>
{
op.AddApi<IDemoAService, DemoService>(); //only generate IDemoAService
op.AddApi<DemoService>(); //Generate all api interface in DemoService
op.AddAssembly(Assembly.GetEntryAssembly()); //Generate all api interface in Assembly
});
}
You can totally use it as a controller.
public interface IDemoService : IAutomaticApi
{
/// <summary>
/// DemoB api
/// </summary>
/// <returns></returns>
[Authorize]
[HttpPost]
[Route("...")]
string UpdateAsync(Guid id, [FromBody] RequestModel model);
}
However, [HttpPost]
[Route]
usually doesn't need to be defined. AutomaticApi will generates Route
and HttpMethod
based on the Method name.
As we all know, some [Attrbute]
may can't put it on interface like this.
[Authorize]
public interface IDemoService : IAutomaticApi
...
We can do some customization.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class MyAuthorizeAttribute : AuthorizeAttribute
{
}
If you think this component can help you, please give me a star.