Skip to content

About

A C# and Razor plugin for Astro.dev

Resources

Contributing

Stars

14 stars

Watchers

0 watching

Forks

Repository files navigation

AstroSharp

Write the coded parts of an Astro site in C# instead of TypeScript.

Components, pages, content loaders, actions, endpoints and middleware can all be authored in C#, while Node and Vite continue to drive the build. This is a build-time and ergonomics project, not a runtime replacement: the .NET SDK runs alongside Node and executes the C# you write, and nothing from .NET ships to the browser.

---
// src/pages/index.astro — an ordinary Astro page
import Card from '../components/Card.razor';
---
<Card title="Written in C#">
  <p>With an Astro-supplied slot.</p>
</Card>
@* src/components/Card.razor *@
@code {
    [Parameter] public string Title { get; set; } = "";
    [Parameter] public RenderFragment? ChildContent { get; set; }
}

<article class="card">
  <h2>@Title</h2>
  @ChildContent
</article>

<style>
  .card { border: 1px solid #512bd4; }
</style>

Requirements

  • Node 20 or newer.
  • The .NET SDK 10 or newer, on your PATH as dotnet.

That is the only extra requirement. The compiled .NET host ships inside the npm package, so there is nothing else to install, no host path to configure, and no separate .NET project to maintain.

Install

npm install astrosharp
// astro.config.mjs
import { defineConfig } from 'astro/config';
import astrosharp from 'astrosharp';

export default defineConfig({
  integrations: [astrosharp()],
});

No options are required. Write C# under src/, next to your .astro files, and run astro build.

Quickstart

A page is a .razor file under src/pages, routed by its file name exactly like .astro:

@* src/pages/hello.razor -> /hello *@
@attribute [AstroLayout("../layouts/Base.astro")]

<h1>Rendered by C#</h1>

An endpoint implements IAstroEndpoint and serves the route its file names:

// src/pages/api/status.json.cs -> /api/status.json
public sealed class Status : IAstroEndpoint
{
    public HttpResponseMessage Get(HttpRequestMessage request) =>
        AstroResults.Ok(new { ok = true, name = request.Query("name") ?? "anonymous" });
}

A content loader implements IContentLoader, and is wired up from src/content.config.ts:

// src/lib/Blog.cs
public sealed class BlogLoader : IContentLoader
{
    public string Collection => "blog";

    public Task<IReadOnlyList<ContentEntry>> LoadAsync(LoaderContext context)
    {
        IReadOnlyList<ContentEntry> entries = [
            new() { Id = "hello", Data = new { title = "Hello" }, Body = "## Rendered by Astro" },
        ];
        return Task.FromResult(entries);
    }
}
// src/content.config.ts
import { defineCollection, z } from 'astro:content';
import { sharpLoader } from 'astrosharp/content';

export const collections = {
  blog: defineCollection({
    loader: sharpLoader('blog'),
    schema: z.object({ title: z.string() }),
  }),
};

An action implements IAstroAction<TInput, TOutput>, and middleware implements IAstroMiddleware. Nothing is registered: the interface is what makes a class one.

NuGet packages go in package.json, since npm rejects a nuget: protocol under dependencies:

{
  "nugetDependencies": { "Humanizer": "nuget:2.14.1" }
}

Services are registered once in a Startup class with ConfigureServices(IServiceCollection) — the ASP.NET Core shape — and injected into all of the above.

One thing to know first

C# components are server-only. They render to HTML and ship no client runtime, so a client:* directive on a .razor component fails the build. Interactive islands stay with a UI framework Astro already supports, on the same page. See Islands architecture.

Documentation

docs/README.md mirrors Astro's own documentation, section for section and page for page. Each page shows the TypeScript, the C# equivalent, and what the C# cannot do.

Start with Installation, or jump to Components, Content collections, Endpoints, Actions or Testing.

How AstroSharp works covers the machinery — the sidecar, the host protocol, the integration surface, dev reload.

Contributing

See CONTRIBUTING.md for the repository layout, how to build it, and how to run the tests.

License

MIT. See LICENSE.

About

A C# and Razor plugin for Astro.dev

Resources

Contributing

Stars

14 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages