Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork159
A framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core.
License
json-api-dotnet/JsonApiDotNetCore
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A framework for buildingJSON:API compliant REST APIs using ASP.NET Core and Entity Framework Core. Includes support for theAtomic Operations extension.
The ultimate goal of this library is to eliminate as much boilerplate as possible by offering out-of-the-box features, such as sorting, filtering, pagination, sparse fieldset selection, and side-loading related resources. You just need to focus on defining the resources and implementing your custom business logic. This library has been designed around dependency injection, making extensibility incredibly easy.
Note
OpenAPI support is nowavailable, currently in preview. Give it a try!
The following steps describe how to create a JSON:API project.
Create a new ASP.NET Core Web API project:
dotnet new webapi --no-openapi --use-controllers --name ExampleJsonApicd ExampleJsonApiInstall the JsonApiDotNetCore package, along with your preferred Entity Framework Core provider:
dotnet add package JsonApiDotNetCoredotnet add package Microsoft.EntityFrameworkCore.Sqlite
Declare your entities, annotated with JsonApiDotNetCore attributes:
[Resource]publicclassPerson:Identifiable<long>{[Attr]publicstring?FirstName{get;set;}[Attr]publicstringLastName{get;set;}=null!;[HasMany]publicISet<Person>Children{get;set;}=newHashSet<Person>();}
Define your
DbContext, seeding the database with sample data:publicclassAppDbContext(DbContextOptions<AppDbContext>options):DbContext(options){publicDbSet<Person>People=>Set<Person>();protectedoverridevoidOnConfiguring(DbContextOptionsBuilderbuilder){builder.UseSqlite("Data Source=SampleDb.db");builder.UseAsyncSeeding(async(dbContext,_,cancellationToken)=>{dbContext.Set<Person>().Add(newPerson{FirstName="John",LastName="Doe",Children={newPerson{FirstName="Baby",LastName="Doe"}}});awaitdbContext.SaveChangesAsync(cancellationToken);});}}
Configure Entity Framework Core and JsonApiDotNetCore in
Program.cs:varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddDbContext<AppDbContext>();builder.Services.AddJsonApi<AppDbContext>(options=>{options.UseRelativeLinks=true;options.IncludeTotalResourceCount=true;});varapp=builder.Build();app.UseRouting();app.UseJsonApi();app.MapControllers();awaitCreateDatabaseAsync(app.Services);app.Run();staticasyncTaskCreateDatabaseAsync(IServiceProviderserviceProvider){awaitusingvarscope=serviceProvider.CreateAsyncScope();vardbContext=scope.ServiceProvider.GetRequiredService<AppDbContext>();awaitdbContext.Database.EnsureDeletedAsync();awaitdbContext.Database.EnsureCreatedAsync();}
Start your API
dotnet run
Send a GET request to retrieve data:
GET http://localhost:5000/people?filter=equals(firstName,'John')&include=children HTTP/1.1
Expand to view the JSON response
{"links": {"self":"/people?filter=equals(firstName,%27John%27)&include=children","first":"/people?filter=equals(firstName,%27John%27)&include=children","last":"/people?filter=equals(firstName,%27John%27)&include=children" },"data": [ {"type":"people","id":"1","attributes": {"firstName":"John","lastName":"Doe" },"relationships": {"children": {"links": {"self":"/people/1/relationships/children","related":"/people/1/children" },"data": [ {"type":"people","id":"2" } ] } },"links": {"self":"/people/1" } } ],"included": [ {"type":"people","id":"2","attributes": {"firstName":"Baby","lastName":"Doe" },"relationships": {"children": {"links": {"self":"/people/2/relationships/children","related":"/people/2/children" } } },"links": {"self":"/people/2" } } ],"meta": {"total":1 }}
The following links explain what this project provides, why it exists, and how you can use it.
- What is JSON:API and why should I use it? (blog, 2017)
- Pragmatic JSON:API Design (video, 2017)
- JSON:API and JsonApiDotNetCore (video, 2021)
- JsonApiDotNetCore Release 4.0 (blog, 2020)
- JSON:API, ASP.NET Core, EmberJS (video, 2017)
- Embercasts: Full Stack Ember with ASP.NET Core (paid course, 2017)
- Theexamples directory provides ready-to-run sample API projects, which are documentedhere.
- Theintegration tests directory covers many advanced use cases, which are documentedhere.This includes topics such as batching, multi-tenancy, authorization, soft-deletion, obfuscated IDs, resource inheritance, alternate routing, custom metadata, error handling and logging.
- TheEmber.js Todo List App showcases a JsonApiDotNetCore API and an Ember.js client with token authentication.
The following chart should help you pick the best version, based on your environment.See also ourversioning policy.
| JsonApiDotNetCore | Status | .NET | Entity Framework Core |
|---|---|---|---|
| master | Preview | 9 | 9 |
| 8 | 8, 9 | ||
| 5.7.0+ | Stable | 9 | 9 |
| 8 | 8, 9 | ||
| 5.5.0-5.6.0 | Stable | 9 | 9 |
| 8 | 8, 9 | ||
| 7 | 7 | ||
| 6 | 6, 7 | ||
| 5.0.3-5.4.0 | Stable | 7 | 7 |
| 6 | 6, 7 | ||
| 5.0.0-5.0.2 | Stable | 6 | 6 |
| 4.x | Stable | 6 | 5 |
| 5 | 5 | ||
| Core 3.1 | 3.1, 5 | ||
| 3.x | Stable | Core 2.x | 2.x |
After each commit to the master branch, a new pre-release NuGet package is automatically published tofeedz.io.To try it out, follow the steps below:
Create a
nuget.configfile in the same directory as your .sln file, with the following contents:<?xml version="1.0" encoding="utf-8"?><configuration> <packageSources> <addkey="json-api-dotnet"value="https://f.feedz.io/json-api-dotnet/jsonapidotnetcore/nuget/index.json" /> <addkey="NuGet"value="https://api.nuget.org/v3/index.json" /> </packageSources></configuration>
In your IDE, browse the list of packages from the
json-api-dotnetfeed. Make sure pre-release packages are included in the list.
Have a question, found a bug or want to submit code changes? See ourcontributing guidelines.
To build the code from this repository locally, run:
dotnet build
Running tests locally requires access to a PostgreSQL database. If you have docker installed, this can be started via:
pwsh run-docker-postgres.ps1
And then to run the tests:
dotnettestAlternatively, to build, run all tests, generate code coverage and NuGet packages:
pwsh Build.ps1
We are grateful to the following sponsors, who provide the team with a no-cost license for using their tools.
Do you like this project? Consider tosponsor, or just reward us by giving our repository a star.
About
A framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core.
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
