An open API service indexing awesome lists of open source software.

https://github.com/keizrivas/aspnetconventions

Automatically standardizes routes, parameters, responses, JSON serialization, and exception handling with zero boilerplate. Drop-in support for MVC Controllers, Minimal APIs, and Razor Pages.
https://github.com/keizrivas/aspnetconventions

api-conventions aspnetcore conventions csharp dotnet json-serialization minimal-api mvc razor-pages rest-api routing web-api

Last synced: about 1 month ago
JSON representation

Automatically standardizes routes, parameters, responses, JSON serialization, and exception handling with zero boilerplate. Drop-in support for MVC Controllers, Minimal APIs, and Razor Pages.

Awesome Lists containing this project

README

          

# AspNetConventions

[![License](https://img.shields.io/github/license/keizrivas/AspNetConventions?label=License&labelColor=383f47)](https://github.com/keizrivas/AspNetConventions?tab=MIT-1-ov-file)
[![Build Status](https://github.com/keizrivas/AspNetConventions/actions/workflows/build.yml/badge.svg)](https://github.com/keizrivas/AspNetConventions/actions/workflows/build.yml)
[![NuGet Version](https://img.shields.io/nuget/v/AspNetConventions.svg?color=00a6f4&logo=nuget&logoColor=959da5&label=NuGet&labelColor=383f47)](https://www.nuget.org/packages/AspNetConventions)
[![Documentation](https://img.shields.io/badge/Docs-AspNetConventions-155dfc?logo=gitbook&logoColor=959da5&labelColor=383f47)](https://keizrivas.github.io/AspNetConventions/)
[![NuGet Downloads](https://img.shields.io/nuget/dt/AspNetConventions.svg?color=9e5dec&label=Downloads&labelColor=383f47)](https://www.nuget.org/packages/AspNetConventions)
[![.NET](https://img.shields.io/badge/.NET-8.0+-512BD4.svg?labelColor=383f47)](https://dotnet.microsoft.com/download)




AspNetConventions

**Convention-driven standardization for ASP.NET Core** - Automatically applies consistent standardization across ASP.NET Core applications — including APIs, MVC, and Razor Pages — with minimal configuration and zero boilerplate.




AspNetConventions

## Why AspNetConventions?

We believe API building should be intuitive. **AspNetConventions** transforms standard ASP.NET setups into a modern API solution by applying smart global behaviors automatically:

- **Universal Endpoint Support** - Consistent URL structure across MVC, Minimal APIs, and Razor Pages.
- **Automatic Route standardization** - Route and parameter names are transformed and bound automatically follow your preferred casing style.
- **Standardized responses** - Uniform JSON formatting and consistent response formatting application-wide.
- **Global exception handling** - Centralized error handling and formatting throughout your codebase.
- **Fully Extensible** - Supports custom converters, mappers, hooks, and response formatters, as well as third-party library compatibility.
- **Zero Runtime Overhead** - Conventions are applied during application startup, no performance impact on requests.

## Quick Start

### Installation

```bash
dotnet add package AspNetConventions
```

### Basic Configuration

```csharp
// Program.cs
using AspNetConventions;

var builder = WebApplication.CreateBuilder(args);

// Apply conventions to MVC Controllers/Razor Pages
builder.Services
.AddControllersWithViews()
.AddAspNetConventions();

var app = builder.Build();

// Apply conventions to Minimal APIs
var api = app.UseAspNetConventions();

api.MapGet("/GetUser/{UserId}", (int UserId) => new { userId = UserId });

app.Run();
```

**That's it!** Your entire application now follows consistent conventions.

## Documentation

Visit the full documentation at: **[keizrivas.github.io/AspNetConventions](https://keizrivas.github.io/AspNetConventions/)**

## Route standardization Examples

### MVC Controllers

**Your Code:**
```csharp
[ApiController]
[Route("Api/[controller]")]
public class UsersController : ControllerBase
{
[HttpGet("[Action]/{id}")]
public ActionResult PublicProfile(int id)
{
return Ok(new { UserId = id, Name = "John Doe" });
}
}
```

**Standardized Route:**
```
GET /api/users/public-profile/{id}
```

---

### Minimal APIs

**Your Code:**
```csharp

var api = app.UseAspNetConventions();

api.MapGet("/WeatherForecasts/City/{name}", (string name) =>
Results.Ok(new { CityName = name, Temperature = 72 }));

```

**Standardized Route:**
```
GET /api/weather-forecasts/city/{name}
```

---

### Razor Pages

**Your Code:**
```csharp
// Pages/UserProfile/Edit.cshtml.cs
public class EditModel : PageModel
{
public void OnGet(int UserId)
{
// Your logic here
}
}
```

```razor
// Pages/UserProfile/Edit.cshtml
@page "{UserId:int}"
@model EditModel

Edit User


```

**Standardized Route:**
```
GET /user-profile/edit/{user-id}
```

## Response standardization Examples

### Success Response

```json
{
"status": "success",
"statusCode": 200,
"message": "User created successfully.",
"data": {
"userId": 1,
"name": "John Doe",
"email": "john.doe@email.com"
},
"metadata": {
"requestType": "PUT",
"timestamp": "0000-00-00T00:00:00.000000Z",
"traceId": "00-ed89d1cc507c35126d6f0e933984f774-99b8b9a3feb75652-00",
"path": "/api/users/{id}"
}
}
```

### Validation Error Response

```json
{
"status": "failure",
"statusCode": 400,
"type": "VALIDATION_ERROR",
"message": "One or more validation errors occurred.",
"errors": [
{
"Email": [
"'Email' is not a valid email address."
]
}
],
"metadata": {
"requestType": "PUT",
"timestamp": "0000-00-00T00:00:00.000000Z",
"traceId": "00-8e5513ae9369648487c2323d9a3508aa-2a8f92c7d45d3f74-00",
"path": "/api/users/{id}"
}
}
```

## Contributing

[Contributions](https://github.com/keizrivas/AspNetConventions/tree/main?tab=contributing-ov-file), [issues](https://github.com/keizrivas/AspNetConventions/issues), and [feature requests](https://github.com/keizrivas/AspNetConventions/pulls) are welcome!

If you believe a convention should be improved or added, feel free to open a [discussion](https://github.com/keizrivas/AspNetConventions/discussions).

## License

This project is licensed under the MIT License. See [LICENSE](https://github.com/keizrivas/AspNetConventions/tree/main?tab=MIT-1-ov-file) for details.