• Stars
    star
    315
  • Rank 132,951 (Top 3 %)
  • Language
    HTML
  • License
    MIT License
  • Created over 1 year ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightweight Razor-based templates for ASP.NET Core without MVC, Razor Pages, or Blazor.

Razor Slices

CI (main) Nuget

Lightweight Razor-based templates for ASP.NET Core without MVC, Razor Pages, or Blazor, optimized for high-performance rendering. Great for returning HTML from Minimal APIs, middleware, etc. Supports .NET 6+

Getting Started

  1. Install the NuGet package into your ASP.NET Core project (.NET 6+):

    > dotnet add package RazorSlices
  2. Create a directory in your project called Slices and add a _ViewImports.cshtml file to it with the following content:

    @inherits RazorSliceHttpResult
    
    @using System.Globalization;
    @using Microsoft.AspNetCore.Razor;
    @using Microsoft.AspNetCore.Http.HttpResults;
    
    @tagHelperPrefix __disable_tagHelpers__:
    @removeTagHelper *, Microsoft.AspNetCore.Mvc.Razor
  3. In the same directory, add a Hello.cshtml file with the following content:

    @inherits RazorSliceHttpResult<DateTime>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Hello from Razor Slices!</title>
    </head>
    <body>
        <p>
            Hello from Razor Slices! The time is @Model
        </p>
    </body>
    </html>
  4. Add a minimal API to return the slice in your Program.cs:

    app.MapGet("/hello", () => Results.Extensions.RazorSlice("/Slices/Hello.cshtml", DateTime.Now));

Installation

NuGet Releases

Nuget

This package is currently available from nuget.org:

> dotnet add package RazorSlices

CI Builds

If you wish to use builds from this repo's main branch you can install them from this repo's package feed.

  1. Create a personal access token for your GitHub account with the read:packages scope with your desired expiration length:

    image

  2. At the command line, navigate to your user profile directory and run the following command to add the package feed to your NuGet configuration, replacing the <GITHUB_USER_NAME> and <PERSONAL_ACCESS_TOKEN> placeholders with the relevant values:

    ~> dotnet nuget add source -n GitHub -u <GITHUB_USER_NAME> -p <PERSONAL_ACCESS_TOKEN> https://nuget.pkg.github.com/DamianEdwards/index.json
  3. You should now be able to add a reference to the package specifying a version from the repository packages feed

  4. See these instructions for further details about working with GitHub package feeds.

Features

The library is still new and features are being actively added.

Currently supported

  • ASP.NET Core 6.0 and above

  • Strongly-typed models (via @inherits RazorSlice<MyModel> or @inherits RazorSliceHttpResult<MyModel>)

  • Razor constructs:

  • DI-activated properties via @inject

  • Asynchronous rendering, i.e. the template can contain await statements, e.g. @await WriteTheThing()

  • Writing UTF8 byte[] values directly to the output

  • Rendering directly to IBufferWriter<byte>, PipeWriter, Stream, TextWriter, StringBuilder, and string outputs, including optimizations for not boxing struct values, zero-allocation rendering of primitives like numbers, etc. (rather than just calling ToString() on everything)

  • Return a slice instance directly as an IResult in minimal APIs via @inherits RazorSliceHttpResult and Results.Extensions.RazorSlice("/Slices/Hello.cshtml")

  • Avoiding slice lookup costs at render time via RazorSlice.ResolveSliceFactory(string sliceName) which returns a SliceFactory delegate that can be cached and then used to directly create an instance of the slice whenever needed. Use SliceFactory<TModel> RazorSlice.ResolveSliceFactory<TModel>(string sliceName, TModel model) for slices with strongly-typed models.

Not yet supported but planned

  • Layouts and sections
  • Rendering slices from slices (aka partials)
  • Support for Hot Reload

Interested in supporting but not sure yet

  • An HTML helper like experience, e.g. @Html.Form(), etc.
  • Getting small updates to the Razor compiler itself to get some usability and performance improvements, e.g.:
    • Don't mark the template's ExecuteAsync method as an async method unless the template contains await statements to save on the async state machine overhead
    • Support compiling static template elements to UTF8 string literals (ReadOnlySpan<char>) instead of string literals to save on the UTF16 to UTF8 conversion during rendering
    • Support disabling the default registered @addtaghelper and @inject directives which rely on MVC

No intention to support

  • Tag Helpers and View Components (they're tied to MVC and are intrinsically "heavy")
  • @model directive (the Razor compiler does not support its use in conjunction with custom base-types via @inherits)
  • @attribute [Authorize] (wrong layer of abstraction for minimal APIs, etc.)

More Repositories

1

MinimalApiPlayground

A place I'm trying out the new ASP.NET Core minimal APIs features.
C#
654
star
2

MiniValidation

A minimalist validation library for .NET built atop the existing features in `System.ComponentModel.DataAnnotations` namespace
C#
339
star
3

TagHelperPack

A set of useful, and possibly opinionated, Tag Helpers for ASP.NET Core
C#
338
star
4

MinimalApis.Extensions

A set of extensions and helpers for working with ASP.NET Core Minimal APIs.
C#
296
star
5

BlazorIdentity

An exploration of recreating the ASP.NET Core Identity UI functionality in Blazor components.
C#
135
star
6

aspnet5-workshop

ASP.NET 5 workshop
106
star
7

TagHelperStarterWeb

ASP.NET 5 Starter Web project template converted to use Tag Helpers
JavaScript
80
star
8

RazorPagesSample

Examples of various ways to build simple CRUD pages using ASP.NET Core Razor Pages (coming in ASP.NET Core 2.0).
C#
77
star
9

aspnetcore-workshop

ASP.NET Core
C#
71
star
10

PiDnx

A sample DNX app for running on Windows 10 IoT Core on Raspberry Pi 2
C#
61
star
11

NDCLondon2013

Demos from my talks at NDC London 2013
JavaScript
55
star
12

AspNetCoreDevJwts

Experimenting with development environment scenarios for JWT authentication.
C#
42
star
13

Nanorm

A tiny data-access helper library for ADO.NET. Linker and native AOT friendly.
C#
41
star
14

AspNetCorePathAuthorization

A middleware to enable path-based authorization in an ASP.NET Core application.
C#
38
star
15

TrimmedTodo

An app to try out trimming stuff in .NET 7
C#
34
star
16

grunt-tsng

A TypeScript pre-processor for AngularJS
JavaScript
29
star
17

MinimalRazorComponents

An exploration of allowing returning rendered Razor Components (aka Blazor Components) from ASP.NET Core Minimal API endpoints.
C#
27
star
18

SignalR-MoveShapeDemo

A simple demo app I use when giving talks on SignalR
JavaScript
21
star
19

KeyTips-jQuery-Plugin

The KeyTips plugin for jQuery (formerly the Access Key Highlighter plugin) provides various options for highlighting access keys on web forms when the user presses their browser's access key shortcut key, e.g. Alt in Internet Explorer and Safari, Shift+Alt in Firefox.
JavaScript
21
star
20

Unobtrusive-jQuery-UI

Unobtrusive wire-up of jQuery UI features using data-ui-* attributes on target elements.
JavaScript
19
star
21

AspNetCoreClientLibManagement

Examples of different techniques for management of client libraries (i.e. JS/CSS libraries/frameworks) to ASP.NET Core projects.
HTML
18
star
22

i18nStarterWeb

ASP.NET 5 Starter Web project template converted to use proposed i18n/l10n system
JavaScript
16
star
23

vNextBowerGrunt

A demonstration of an approach for integration of Bower and Grunt into an ASP.NET vNext project.
JavaScript
16
star
24

dotnetConf2014

Demos from my dotnetConf 2014 SignalR talk http://channel9.msdn.com/Events/dotnetConf/2014/SignalR
JavaScript
15
star
25

SystemWebSdk

Exploration of using new .NET SDK-style projects with ASP.NET (System.Web) projects
JavaScript
15
star
26

SignalR-2.x-demo

Demos for SignalR 2.0 and upcoming 2.x releases
JavaScript
14
star
27

BlazingPageHead

Example of Blazor components for managing page head content.
HTML
13
star
28

Ignite2022

Demos for talk at MS Ignite 2022 with David Fowler
C#
12
star
29

YouTubePlaylistCopier

A one-page ASP.NET Core site that allows for copying items from one YouTube playlist to another.
C#
11
star
30

ndcoslo2016

Demos for NDC Oslo 2016
C#
11
star
31

TrackDirty-jQuery-Plugin

The TrackDirty plugin for jQuery tracks changes to form fields. When changes are detected, the field's class name is updated so as to allow styling. A callback function can be passed which will be called when a field is marked dirty. You can also query dirty fields using the added ":dirty" pseudo class.
JavaScript
11
star
32

aspConf2012

Demo code from talks at aspConf2012
JavaScript
10
star
33

AspNetCoreVbHybrid

Example of ASP.NET Core application with logic written in Visual Basic
C#
8
star
34

WebFormsModelBindingExtensions

Sample extensions for ASP.NET Web Forms Model Binding
C#
8
star
35

RazorPagesLocalization

Sample of fixed Razor Pages localization in ASP.NET Core 2.0.0 application
C#
7
star
36

RealTimeEntityFramework

Some ideas for real-time extensions to Entity Framework
C#
7
star
37

MusicStore

JavaScript
6
star
38

SimpleMvcHack

Some hacky ideas for simplifying MVC for page-focused scenarios.
C#
6
star
39

CookieConsentUI

Example cookie consent UI for ASP.NET Core project templates, coming in ASP.NET Core 2.1
C#
6
star
40

dotnet-new2

A .NET Core CLI command to create new .NET Core projects using templates installed via NuGet packages.
C#
5
star
41

jQueryVsDocBuilder

Utility for creating jQuery vsdoc files for Visual Studio IntelliSense
JavaScript
4
star
42

GittR

Simple issue management board for GitHub projects with real-time updating using ASP.NET SignalR.
JavaScript
4
star
43

UrlRewriteToPublic

Use URL Rewrite in IIS to only serve documents from /public folder
C#
3
star
44

MoveShapeCore

Move Shape for ASP.NET Core SignalR
JavaScript
3
star
45

Northwind

Northwind packages for Entity Framework Code First and VS11 Database projects
3
star
46

UnauthorizedApis

Exploration of potential change to ASP.NET Core behavior for what happens when accessing unauthorized API endpoints.
C#
3
star
47

CustomCultureExample

Demonstrates the issue of trying to localize for custom cultures in .NET Core/.NET 5+ projects.
HTML
3
star
48

AspireStarterDb

C#
3
star
49

MusicStoreSpa

2
star
50

BlazorFormFun

Reproducing patterns, etc. with new Blazor form handling in ASP.NET Core 8
CSS
2
star
51

HeritageTemplates

It's the .NET templates the way you remember them.
C#
2
star
52

AngleBracketsSpring2013

Deoms from my presentations at AngleBrackets/DevIntersections Spring 2013
JavaScript
2
star
53

RazorPages

C#
1
star
54

RazorFlushing

Examples of preemptive flushing from ASP.NET Core Razor
C#
1
star