title: Basic JSON APIs with Route-to-code in ASP.NET Core
author: jamesnk
description: Learn how to use Route-to-code and JSON extension methods to create lightweight JSON web APIs.
monikerRange: '>= aspnetcore-5.0'
ms.author: jamesnk
ms.custom: mvc
ms.date: 11/30/2020
uid: web-api/route-to-code
---
# Basic JSON APIs with Route-to-code in ASP.NET Core
ASP.NET Core supports a number of ways of creating JSON web APIs:
* [ASP.NET Core web API](xref:web-api/index) provides a complete framework for creating APIs. A service is created by inheriting from <xref:Microsoft.AspNetCore.Mvc.ControllerBase>. Some features provided by the framework include model binding, validation, content negotiation, input and output formatting, and OpenAPI.
* Route-to-code is a non-framework alternative to ASP.NET Core web API. Route-to-code connects [ASP.NET Core routing](xref:fundamentals/routing) directly to your code. Your code reads from the request and writes the response. Route-to-code doesn't have web API's advanced features, but there's also no configuration required to use it.
Route-to-code is a good approach when building small and basic JSON web APIs.
## Create JSON web APIs
ASP.NET Core provides helper methods that ease the creation of JSON web APIs:
*<xref:Microsoft.AspNetCore.Http.HttpRequestJsonExtensions.HasJsonContentType%2A> checks the `Content-Type` header for a JSON content type.
*<xref:Microsoft.AspNetCore.Http.HttpRequestJsonExtensions.ReadFromJsonAsync%2A> reads JSON from the request and deserializes it to the specified type.
*<xref:Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.WriteAsJsonAsync%2A> writes the specified value as JSON to the response body and sets the response content type to `application/json`.
Lightweight, route-based JSON APIs are specified in `Startup.cs`. The route and the API logic are configured in <xref:Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints%2A> as part of an app's request pipeline.
* Default serialization options can be configured with <xref:Microsoft.AspNetCore.Http.Json.JsonOptions> in the `Startup.ConfigureServices` method.
*`WriteAsJsonAsync` and `ReadFromJsonAsync` have overloads that accept a <xref:System.Text.Json.JsonSerializerOptions> object. This options object overrides the default options.
Route-to-code supports authentication and authorization. Attributes, such as `[Authorize]` and `[AllowAnonymous]`, can't be placed on endpoints that map to a request delegate. Instead, authorization metadata is added using the <xref:Microsoft.AspNetCore.Builder.AuthorizationEndpointConventionBuilderExtensions.RequireAuthorization%2A> and <xref:Microsoft.AspNetCore.Builder.AuthorizationEndpointConventionBuilderExtensions.AllowAnonymous%2A> extension methods.
[Dependency injection (DI)](xref:fundamentals/dependency-injection) using a constructor isn't possible with Route-to-code. Web API creates a controller for you with services injected into the constructor. A type isn't created when an endpoint is executed, so services must be resolved manually.
Route-based APIs can use <xref:System.IServiceProvider> to resolve services:
* Transient and scoped lifetime services, such as `DbContext`, must be resolved from [HttpContext.RequestServices](xref:Microsoft.AspNetCore.Http.HttpContext.RequestServices) inside an endpoint's request delegate.
* Singleton lifetime services, such as `ILogger`, can be resolved from [IEndpointRouteBuilder.ServiceProvider](xref:Microsoft.AspNetCore.Routing.IEndpointRouteBuilder.ServiceProvider). Services can be resolved outside of request delegates and shared between endpoints.
APIs that use DI extensively should consider using an ASP.NET Core app type that supports DI. For example, ASP.NET Core web API. Service injection using a controller's constructor is easier than manually resolving services.
Route-based APIs don't have to be located in `Startup.cs`. APIs can be placed in other files and mapped at startup with `UseEndpoints`. This approach reduces the startup configuration file size.