AspNetCore.InertiaCore 0.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package AspNetCore.InertiaCore --version 0.0.2                
NuGet\Install-Package AspNetCore.InertiaCore -Version 0.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AspNetCore.InertiaCore" Version="0.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AspNetCore.InertiaCore --version 0.0.2                
#r "nuget: AspNetCore.InertiaCore, 0.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AspNetCore.InertiaCore as a Cake Addin
#addin nuget:?package=AspNetCore.InertiaCore&version=0.0.2

// Install AspNetCore.InertiaCore as a Cake Tool
#tool nuget:?package=AspNetCore.InertiaCore&version=0.0.2                

Inertia.js ASP.NET Adapter

NuGet NuGet License

Attribution

This library is heavily inspired by Nothing-Works/inertia-aspnetcore, but it has some errors fixed and its usage is more similar to the official adapters'.

What was added

  • Cycle-safe model relations data serialization.
  • Validation error handling.
  • Better shared data integration.
  • Props and shared props are merged instead of being separated.
  • Fixed PATCH, PUT, DELETE redirection not working properly.

Installation

  1. Using Package Manager: PM> Install-Package AspNetCore.InertiaCore
  2. Using .NET CLI: dotnet add package AspNetCore.InertiaCore

Getting started

You need to add few lines to the Program.cs or Starup.cs file.

using InertiaCore.Extensions;

[...]

builder.Services.AddControllersWithViews()
    .AddInertiaOptions(); // Configure MVC options
builder.Services.AddInertia();

[...]

app.UseInertia();

Usage

Frontend

Create a file /Views/App.cshtml.

@using InertiaCore
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title inertia>My App</title>
</head>
<body>
@Inertia.Html(Model)

<script src="/js/app.js"></script>
</body>
</html>

You can change the root view file using

app.UseInertia();
Inertia.SetRootView("~/Views/Main.cshtml");

in your Program.cs or Startup.cs file.

Backend

To pass data to a page component, use Inertia.Render().

    public async Task<IActionResult> Index()
    {
        var posts = await _context.Posts.ToListAsync();
        
        var data = new
        {
            Posts = posts,
        };
        
        return Inertia.Render("Posts", data);
    }

To make a form endpoint, remember to add [FromBody] to your model parameter, because the request data is passed using JSON.

    [HttpPost]
    public async Task<IActionResult> Create([FromBody] Post post)
    {
        if (!ModelState.IsValid)
        {
            // The validation errors are passed automatically.
            return await Index();
        }
        
        _context.Add(post);
        await _context.SaveChangesAsync();
        
        return RedirectToAction("Index");
    }

Features

Shared data

You can add some shared data to your views using for example middlewares.

using InertiaCore;

[...]

app.Use(async (context, next) =>
{
    var userId = context.Session.GetInt32("userId");
    
    Inertia.Share("auth", new
    {
        UserId = userId
    });
    
    // Or
    
    Inertia.Share(new Dictionary<string, object?>
    {
        ["auth"] => new
        {
            UserId = userId
        }
    });
});
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AspNetCore.InertiaCore:

Package Downloads
AbanoubNassem.Trinity

Trinity is a powerful Single-Page Application (SPA) administration tool that is designed to streamline common administrative tasks and enhance the productivity of developers. With its feature-rich and beautifully-designed interface, built using C# and ASP.NET, Trinity makes it easy to manage your website's backend with ease.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.9 169 10/18/2024
0.0.8 1,131 8/18/2023
0.0.7 600 3/10/2023
0.0.5 284 2/5/2023
0.0.4 414 1/27/2023
0.0.3 335 12/19/2022
0.0.2 328 12/19/2022
0.0.1 323 12/18/2022