Skip to content

Commit ba804d5

Browse files
committed
o.0.2
1 parent ce9d988 commit ba804d5

File tree

5 files changed

+92
-47
lines changed

5 files changed

+92
-47
lines changed

Inertia.AspNetCore/Configure.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Reflection;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.FileProviders;
4+
5+
namespace Inertia.AspNetCore
6+
{
7+
public static class Configure
8+
{
9+
public static IMvcBuilder UseInertia(this IMvcBuilder app)
10+
{
11+
app.AddRazorRuntimeCompilation(opts =>
12+
{
13+
opts.FileProviders.Add(
14+
new EmbeddedFileProvider(typeof(Configure).GetTypeInfo().Assembly));
15+
});
16+
17+
return app;
18+
}
19+
}
20+
}

Inertia.AspNetCore/Inertia.AspNetCore.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net5.0</TargetFramework>
55
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
66
<PackageId>Inertia.AspNetCore</PackageId>
7-
<Version>0.0.1</Version>
7+
<Version>0.0.2</Version>
88
<Description>A simple AspNetCore adapter for Inertia.js. https://inertiajs.com</Description>
99
<Authors>Frédéric Choquette</Authors>
1010
<PackageProjectUrl></PackageProjectUrl>
@@ -31,4 +31,8 @@
3131
</None>
3232
</ItemGroup>
3333

34+
<ItemGroup>
35+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.10" />
36+
</ItemGroup>
37+
3438
</Project>

Inertia.AspNetCore/Inertia.cs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,9 @@
1-
using System;
2-
using System.Text.Json;
3-
using Microsoft.AspNetCore.Http;
4-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Mvc;
52

63
namespace Inertia.AspNetCore
74
{
8-
9-
internal class InertiaData
5+
public static class InertiaView
106
{
11-
public string component { get; set; }
12-
public string url { get; set; }
13-
public string version { get; set; }
14-
public object props { get; set; }
15-
}
16-
public class Inertia : Controller
17-
{
18-
public IActionResult Render(HttpRequest request, HttpResponse response ,string component, object props)
19-
{
20-
InertiaData data = new InertiaData
21-
{
22-
component = component,
23-
props = props,
24-
url = request.Path.Value,
25-
version = Guid.NewGuid().ToString().Replace("-", string.Empty)
26-
};
27-
28-
if (request.Headers.ContainsKey("X-Inertia"))
29-
{
30-
if (request.Headers["X-Inertia"].ToString() == "true")
31-
{
32-
response.Headers["Vary"] = "Accept";
33-
response.Headers["X-Inertia"] = "True";
34-
return Json(data);
35-
}
36-
}
37-
38-
ViewBag.Data = JsonSerializer.Serialize<InertiaData>(data);
39-
return View("inertia");
40-
}
7+
public static IActionResult Render(string component, object props) => new InertiaResult(component, props);
418
}
429
}

Inertia.AspNetCore/Result.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
6+
7+
namespace Inertia.AspNetCore
8+
{
9+
internal class InertiaData
10+
{
11+
public string component { get; set; }
12+
public string url { get; set; }
13+
public string version { get; set; }
14+
public object props { get; set; }
15+
}
16+
17+
internal class InertiaResult : IActionResult
18+
{
19+
private readonly string _component;
20+
private readonly object _props;
21+
22+
public InertiaResult(string component, object props)
23+
{
24+
_component = component;
25+
_props = props;
26+
}
27+
28+
public async Task ExecuteResultAsync(ActionContext context)
29+
{
30+
InertiaData data = new InertiaData
31+
{
32+
component = _component,
33+
props = _props,
34+
url = context.HttpContext.Request.Path.Value + context.HttpContext.Request.QueryString.Value,
35+
version = Guid.NewGuid().ToString().Replace("-", string.Empty)
36+
};
37+
38+
if (context.HttpContext.Request.Headers.ContainsKey("X-Inertia"))
39+
{
40+
if (context.HttpContext.Request.Headers["X-Inertia"].ToString() == "true")
41+
{
42+
context.HttpContext.Response.Headers["Vary"] = "Accept";
43+
context.HttpContext.Response.Headers["X-Inertia"] = "True";
44+
await new JsonResult(data).ExecuteResultAsync(context);
45+
return;
46+
}
47+
}
48+
49+
var render = new ViewResult
50+
{
51+
ViewName = "inertia",
52+
ViewData = new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary()) { { "Data", JsonSerializer.Serialize<InertiaData>(data) } }
53+
};
54+
55+
await render.ExecuteResultAsync(context);
56+
}
57+
}
58+
}

README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
## Prerequisite
77
1. .NET Core v5
8-
2. A ASP.NET Core MVC project
9-
3. Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation installed via .NET CLI or Package Manger
10-
1. Package Manager: PM> Install-Package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
11-
2. .NET CLI: dotnet add package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
8+
2. An ASP.NET Core MVC project
129

1310
## Install
1411
1. Package Manager: PM> Install-Package Inertia.AspNetCore
@@ -17,21 +14,20 @@
1714
## Usage
1815
1. Setup Startup.cs
1916
```c#
17+
using Inertia.AspNetCore;
18+
2019
public void ConfigureServices(IServiceCollection services)
2120
{
22-
services.AddControllersWithViews().AddRazorRuntimeCompilation(opts =>
23-
{
24-
opts.FileProviders.Add(new EmbeddedFileProvider(typeof(Inertia.AspNetCore.Inertia).GetTypeInfo().Assembly));
25-
})
21+
services.AddControllersWithViews().UserInertia();
2622
}
2723
```
2824
2. In your controller
2925
````c#
30-
private readonly Inertia.AspNetCore.Inertia _inertia = new Inertia.AspNetCore.Inertia();
26+
using Inertia.AspNetCore;
3127

3228
public IActionResult Index()
3329
{
34-
return _inertia.Render(Request, Response, "Index", new
30+
return InertiaView.Render("Index", new
3531
{
3632
UserId = 1
3733
});

0 commit comments

Comments
 (0)