MicroAPI.Net
1.0.1.3
See the version list below for details.
dotnet add package MicroAPI.Net --version 1.0.1.3
NuGet\Install-Package MicroAPI.Net -Version 1.0.1.3
<PackageReference Include="MicroAPI.Net" Version="1.0.1.3" />
<PackageVersion Include="MicroAPI.Net" Version="1.0.1.3" />
<PackageReference Include="MicroAPI.Net" />
paket add MicroAPI.Net --version 1.0.1.3
#r "nuget: MicroAPI.Net, 1.0.1.3"
#:package MicroAPI.Net@1.0.1.3
#addin nuget:?package=MicroAPI.Net&version=1.0.1.3
#tool nuget:?package=MicroAPI.Net&version=1.0.1.3
MicroAPI.Net
Read this in other languages: 简体中文
MicroAPI.Net is a lightweight .NET source generator that automatically creates ASP.NET Core controllers from interfaces and facade classes. With simple attribute annotations, you can quickly build RESTful APIs without writing repetitive controller code.
Features
- 🚀 Zero Boilerplate - Automatically generate controllers from interfaces or facade classes
- 🔄 Compile-time Generation - Generate code at compile time with no runtime overhead
- 🛠️ Highly Configurable - Customize controller names, routes, and namespaces
- 📦 Lightweight - Minimal dependencies, focused on a single responsibility
- 🔍 Type-safe - Leverage compile-time type checking for API consistency
Installation
Install via NuGet Package Manager:
dotnet add package MicroAPI.Net
Quick Start
1. Create a Service Interface
public interface IUserService
{
Task<User> GetUserAsync(int id);
Task<List<User>> GetAllUsersAsync();
Task<User> CreateUserAsync(string name, int age);
Task<bool> DeleteUserAsync(int id);
}
2. Implement the Service
public class UserService : IUserService
{
// Implement interface methods
}
3. Create a Facade Class or Use Interface Annotations
Option 1: Using a Facade Class
[HttpFacade("User")]
public class UserServiceFacade : IUserService
{
[Get("{id}")]
public Task<User> GetUserAsync(int id) => null!;
[Get("")]
public Task<List<User>> GetAllUsersAsync() => null!;
[Post]
public Task<User> CreateUserAsync(string name, int age) => null!;
[Delete("{id}", MethodName = "MyDeleteUser")]
public bool DeleteUser(int id) => false;
public void Debug() { }
}
Generated code:
// UserController.g.cs
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
namespace MicroAPI.Sample.Facades.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
private readonly MicroAPI.Sample.Services.IUserService _service;
public UserController(MicroAPI.Sample.Services.IUserService service)
{
_service = service;
}
[HttpGet("{id}")]
public System.Threading.Tasks.Task<MicroAPI.Sample.Models.User> GetUserAsync([FromRoute] int id)
=> _service.GetUserAsync(id);
[HttpGet("")]
public System.Threading.Tasks.Task<System.Collections.Generic.List<MicroAPI.Sample.Models.User>> GetAllUsersAsync()
=> _service.GetAllUsersAsync();
[HttpPost("CreateUserAsync")]
public System.Threading.Tasks.Task<MicroAPI.Sample.Models.User> CreateUserAsync([FromBody] CreateUserAsyncRequest request)
=> _service.CreateUserAsync(request.name, request.age);
[HttpDelete("{id}")]
public bool MyDeleteUser([FromRoute] int id)
=> _service.DeleteUser(id);
}
}
// UserDtos.g.cs
using System;
namespace MicroAPI.Sample.Facades.Controllers
{
public record CreateUserAsyncRequest(string name, int age);
}
Option 2: Using an Interface
[HttpFacade(DtoNamespace = "MyApp.Api.Models")]
public interface IUserService
{
[Get("{id}")]
Task<User> GetUserAsync(int id);
[Get("")]
Task<List<User>> GetAllUsersAsync();
[Post]
Task<User> CreateUserAsync(string name, int age);
[Delete("{id}", MethodName = "MyDeleteUser")]
bool DeleteUser(int id);
void Debug();
}
Generated code:
// UserServiceController.g.cs
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using MyApp.Api.Models;
namespace MicroAPI.Sample.Services.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserServiceController : ControllerBase
{
private readonly MicroAPI.Sample.Services.IUserService _service;
public UserServiceController(MicroAPI.Sample.Services.IUserService service)
{
_service = service;
}
[HttpGet("{id}")]
public System.Threading.Tasks.Task<MicroAPI.Sample.Models.User> GetUserAsync([FromRoute] int id)
=> _service.GetUserAsync(id);
[HttpGet("")]
public System.Threading.Tasks.Task<System.Collections.Generic.List<MicroAPI.Sample.Models.User>> GetAllUsersAsync()
=> _service.GetAllUsersAsync();
[HttpPost("CreateUserAsync")]
public System.Threading.Tasks.Task<MicroAPI.Sample.Models.User> CreateUserAsync([FromBody] CreateUserAsyncRequest request)
=> _service.CreateUserAsync(request.name, request.age);
[HttpDelete("{id}")]
public bool MyDeleteUser([FromRoute] int id)
=> _service.DeleteUser(id);
}
}
// UserServiceDtos.g.cs
using System;
namespace MyApp.Api.Models
{
public record CreateUserAsyncRequest(string name, int age);
}
Advanced Configuration
Custom Controller Name
[HttpFacade("CustomName")]
public interface IUserService { ... }
Custom Namespaces
[HttpFacade(ControllerNamespace = "MyApp.Api.Controllers", DtoNamespace = "MyApp.Api.Models")]
public interface IUserService { ... }
Custom Method Name
[Get(MethodName = "FindById")]
Task<User> GetUserAsync(int id);
Background and Motivation
With the rise of microservices, Domain-Driven Design (DDD), and similar architectures, Controller layers have become increasingly thin. In some microservice clusters, unified gateways and middleware handle responses. In these systems, each endpoint often simply forwards requests to a Service, no longer strictly following RESTful conventions or fully utilizing HttpStatusCode semantics. In fact, HttpStatusCode semantics are often insufficient to express rich business scenarios, making these APIs more like WebRPC.
MicroAPI.Net was created to address these challenges in ASP.NET Core API development:
Repetitive Controller Code: Traditional API controllers often contain significant boilerplate code that merely forwards requests to the service layer.
Separation of Concerns: Maintaining a clear separation between API contracts and business logic implementation is challenging with standard controller patterns.
API Consistency: Ensuring consistent API design across multiple controllers and endpoints is difficult without standardized approaches.
Maintenance Cost: Changes to service interfaces typically require corresponding controller code changes, potentially leading to inconsistencies.
MicroAPI.Net addresses these issues by automatically generating controller code from service interfaces or facade classes, ensuring the API layer accurately reflects service contracts while eliminating boilerplate code.
Contributing
Contributions are welcome! Please see the Contributing Guidelines for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
2.0.4.15 | 121 | 7/31/2025 |
2.0.3.14 | 146 | 7/30/2025 |
2.0.2.13 | 117 | 7/27/2025 |
2.0.1.12 | 278 | 7/7/2025 |
2.0.0.11 | 142 | 7/6/2025 |
1.2.1.10 | 219 | 7/3/2025 |
1.2.0.9 | 142 | 7/3/2025 |
1.1.0.8 | 264 | 6/28/2025 |
1.0.1.7 | 68 | 6/28/2025 |
1.0.1.6 | 69 | 6/28/2025 |
1.0.1.5 | 154 | 6/27/2025 |
1.0.1.4 | 136 | 6/26/2025 |
1.0.1.3 | 131 | 6/26/2025 |
1.0.1.2 | 139 | 6/26/2025 |
1.0.0.1 | 145 | 6/26/2025 |