Pricing is a product decision, but it lives in code. The moment you have a Free and a Pro plan, every feature asks the same two questions: is this tenant allowed to use it, and have they used it up? Scatter those checks across controllers and changing a price page becomes a week of work. This guide puts plans in one place, enforces them per tenant, and turns "limit reached" into an upgrade prompt. Examples target ASP.NET Core on .NET 10 with EF Core 10.
Decide what a plan controls
A plan controls two kinds of things, and they behave differently:
| Kind | Example | Check |
|---|---|---|
| Feature | Custom domain, API access, export to Excel | Yes or no |
| Limit | 3 projects, 1,000 API calls a month, 5 team members | Current usage against a number |
Keep plan definitions in configuration, not the database, while you have a handful of plans. Prices change; a config change and a deploy is easier to review than a data migration.
"Plans": {
"free": { "Features": [], "Limits": { "Projects": 3, "Members": 1, "ApiCallsPerMonth": 1000 } },
"pro": { "Features": [ "ApiAccess", "Export" ], "Limits": { "Projects": 25, "Members": 5, "ApiCallsPerMonth": 50000 } },
"business": { "Features": [ "ApiAccess", "Export", "Sso" ], "Limits": { "Projects": 0, "Members": 50, "ApiCallsPerMonth": 500000 } }
}A limit of 0 means unlimited. Write that down in the options class so nobody reads it as "none allowed".
Model plans in C#
public enum Feature { ApiAccess, Export, Sso }
public enum Limit { Projects, Members, ApiCallsPerMonth }
public sealed class PlanDefinition
{
public HashSet<Feature> Features { get; init; } = [];
/// <summary>0 means unlimited.</summary>
public Dictionary<Limit, int> Limits { get; init; } = [];
}
public sealed class PlansOptions : Dictionary<string, PlanDefinition>;builder.Services.AddOptions<PlansOptions>()
.Bind(builder.Configuration.GetSection("Plans"))
.Validate(p => p.ContainsKey("free"), "A 'free' plan is required.")
.ValidateOnStart();The tenant row stores only its plan key ("pro") and, if you sell through a payment provider, the subscription ID. Everything else comes from the definition.
One service answers every plan question
public sealed record LimitCheck(bool Allowed, int Used, int Max);
public interface IPlanService
{
Task<bool> HasFeatureAsync(Feature feature, CancellationToken ct);
Task<LimitCheck> CheckLimitAsync(Limit limit, CancellationToken ct);
}
public sealed class PlanService(
ITenantContext tenant, IOptions<PlansOptions> plans, AppDbContext db, IUsageCounter usage) : IPlanService
{
private PlanDefinition Plan =>
plans.Value.TryGetValue(tenant.PlanKey, out var plan) ? plan : plans.Value["free"];
public Task<bool> HasFeatureAsync(Feature feature, CancellationToken ct) =>
Task.FromResult(Plan.Features.Contains(feature));
public async Task<LimitCheck> CheckLimitAsync(Limit limit, CancellationToken ct)
{
var max = Plan.Limits.GetValueOrDefault(limit);
var used = limit switch
{
Limit.Projects => await db.Projects.CountAsync(p => p.TenantId == tenant.Id, ct),
Limit.Members => await db.Members.CountAsync(m => m.TenantId == tenant.Id, ct),
Limit.ApiCallsPerMonth => await usage.GetMonthlyAsync(tenant.Id, "api", ct),
_ => 0,
};
return new LimitCheck(max == 0 || used < max, used, max);
}
}Unknown plan keys fall back to Free. A tenant whose subscription lapsed or whose row has a typo gets the safest plan, not every feature.
Enforce features with an endpoint filter
Feature checks belong at the edge, before the handler runs. An endpoint filter keeps them declarative:
public static class PlanEndpointExtensions
{
public static RouteHandlerBuilder RequireFeature(this RouteHandlerBuilder builder, Feature feature) =>
builder.AddEndpointFilter(async (context, next) =>
{
var plans = context.HttpContext.RequestServices.GetRequiredService<IPlanService>();
if (await plans.HasFeatureAsync(feature, context.HttpContext.RequestAborted))
return await next(context);
return Results.Problem(
statusCode: StatusCodes.Status402PaymentRequired,
title: "Upgrade required",
detail: $"{feature} is available on a higher plan.",
extensions: new Dictionary<string, object?> { ["feature"] = feature.ToString(), ["upgradeUrl"] = "/billing" });
});
}
app.MapGet("/api/reports/export", ExportReport).RequireFeature(Feature.Export);402 Payment Required is rarely used, which makes it a clear signal: the front end can catch it and show the upgrade dialog instead of a generic error.
Enforce limits where things are created
Limits are checked at the moment of creation, inside the service, because only the service knows a project is about to be added:
public async Task<Result<ProjectDto>> CreateAsync(CreateProject input, CancellationToken ct)
{
var check = await plans.CheckLimitAsync(Limit.Projects, ct);
if (!check.Allowed)
return Result.Fail<ProjectDto>($"Your plan includes {check.Max} projects. Upgrade to add more.");
// ... create the project
}Two requests at the same moment can both pass the check. For a project count that is acceptable: one extra project is not worth a lock. For anything billed per unit, such as API calls, count with an atomic increment (UPDATE ... SET Count = Count + 1 OUTPUT inserted.Count) and compare the result instead.
Show limits before people hit them
The best limit message is one the user never needs. Show usage where it matters ("2 of 3 projects used") and offer the upgrade at 80%, not at 100%. The same CheckLimitAsync call feeds the UI, so the number on screen and the rule on the server never disagree.
What happens on downgrade
Decide this before your first downgrade, not during it. The usual rule: nothing is deleted. A tenant with 10 projects who moves to Free keeps them all, read-only, and can't create new ones until they are under the limit. Deleting customer data because a card expired is the fastest way to lose them for good.
Wrapping up
Define plans in one place, give every plan question one service, check features at the endpoint and limits at creation, and return a response the front end can turn into an upgrade prompt. When the pricing page changes, you change a config file, not twenty controllers.