Skip to content

Instantly share code, notes, and snippets.

Working from home

Damian Edwards DamianEdwards

Working from home
View GitHub Profile
DamianEdwards / Output.txt
Created January 4, 2023 01:22
PathGenerator utility for ASP.NET Core that generates URL paths (e.g. for links) using patterns that follow usual route syntax and provided values.
View Output.txt
** Value Objects **
** Ordinal **
DamianEdwards / Index.cshtml
Last active November 19, 2022 00:10
Example ASP.NET Core Razor Page in C# that calls a quotes REST API with fallback to offline quotes in case of failure
View Index.cshtml
@model IndexModel
ViewData["Title"] = "Home page";
<div class="card">
<div class="card-body">
@await Model.GetInspirationalQuote()
<div class="card-footer">
DamianEdwards / Program.cs
Created November 5, 2022 04:02
GZip a string and base64 encode the result in C# (.NET 7)
View Program.cs
using System.Buffers;
using System.Buffers.Text;
using System.Diagnostics;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
DamianEdwards / Program.cs
Created October 20, 2022 22:07
Disable the `AuthenticationHandler`'s new behavior of setting the default authentication scheme to the single scheme registered when there is only one
View Program.cs
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at
DamianEdwards / GetProjectVersion.targets
Created October 18, 2022 17:35
Example of .NET console app (that could be made into a cmd line tool) that uses MSBuild to extract project version
View GetProjectVersion.targets
<!-- Put this in sub-directory of the project named 'assets' -->
<Target Name="_ExtractVersionMetadata">
<WriteLinesToFile File="$(_ProjectVersionMetadataFile)" Lines="$(Version)" />
DamianEdwards / Program.cs
Created August 6, 2022 00:32
Some SwashBuckling with .NET 7 rc.1
View Program.cs
using System.Buffers.Text;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
DamianEdwards / WebApplicationAuthorizationBuilder.cs
Created June 16, 2022 19:05
Prototype pseudo-implementation of WebApplicationAuthorizationBuilder
View WebApplicationAuthorizationBuilder.cs
public class WebApplicationAuthorizationBuilder
private readonly WebApplicationBuilder _builder;
public WebApplicationAuthorizationBuilder(WebApplicationBuilder builder)
_builder = builder;
public WebApplicationAuthorizationBuilder AddPolicy(
DamianEdwards /
Last active November 22, 2022 19:24
ASP.NET Core middleware & endpoint routing type & extension hierarchy
  • IApplicationBuilder Used to configure the application middleware pipeline
    • Map() -> IApplicationBuilder
    • MapWhen() -> IApplicationBuilder
    • Run() -> void
    • Use() -> IApplicationBuilder
    • UseMiddleware() -> IApplicationBuilder
    • UsePathBase() -> IApplicationBuilder
    • UseRouting() -> IApplicationBuilder
    • UseRouter(Action<IRouteBuilder> action) -> IApplicationBuilder
    • UseEndpointPoints() -> IApplicationBuilder
DamianEdwards / Program.cs
Last active March 6, 2022 04:20
Endpoint Groups
View Program.cs
app.MapGroup("/apis", apis =>
var problemJsonMediaType = new MediaTypeHeaderValue("application/problem+json");
apis.MapGet("/error", (HttpContext context) =>
// Get exception details
var error = context.Features.Get<IExceptionHandlerFeature>()?.Error;
var badRequestEx = error as BadHttpRequestException;
View Program.cs
// Now
app.MapGet("/todos/{id}", async (int id, TodoDb db) =>
await db.Todos.FindAsync(id)
is Todo todo
? Results.Ok(todo)
: Results.NotFound())