Skip to content

Instantly share code, notes, and snippets.


Seb Nilsson sebnilsson

View GitHub Profile

Keybase proof

I hereby claim:

  • I am sebnilsson on github.
  • I am sebnilsson ( on keybase.
  • I have a public key ASCWYybMp3SXcq0oy9xFjsdWBwizgfPCOEE1sCL7ke-Gmwo

To claim this, I am signing this object:

sebnilsson / HtmlEncodeTagHelper.cs
Last active Jan 18, 2019
Render everything inside tag as encoded HTML
View HtmlEncodeTagHelper.cs
[HtmlTargetElement("html-encode", TagStructure = TagStructure.NormalOrSelfClosing)]
public class HtmlEncodeTagHelper : TagHelper
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
var childContent = output.Content.IsModified
? output.Content.GetContent()
: (await output.GetChildContentAsync()).GetContent();
string encodedChildContent = WebUtility.HtmlEncode(childContent ?? string.Empty);
sebnilsson / HashAlgorithmExtensions.cs
Created Dec 30, 2017
Compute hash in .NET using async await
View HashAlgorithmExtensions.cs
public static class HashAlgorithmExtensions
private const int BufferSize = 4096;
public static async Task<byte[]> ComputeHashAsync(this HashAlgorithm hash, Stream inputStream)
if (hash == null) throw new ArgumentNullException(nameof(hash));
if (inputStream == null) throw new ArgumentNullException(nameof(inputStream));
sebnilsson / Autofac.AutoMapper.Program.cs
Last active Feb 5, 2019
Registering Autofac & AutoMapper Circularly
View Autofac.AutoMapper.Program.cs
public static void Main(string[] args)
var components = RegisterComponents();
private static IContainer RegisterComponents()
var builder = new ContainerBuilder();
View DateTimeRfc3339Extensions.cs
public static class DateTimeRfc3339Extensions
public static string ToRfc3339String(this DateTime dateTime)
string rfc3339 = dateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo);
return rfc3339;
sebnilsson / RouteCollectionAreasExtensions.cs
Created Dec 2, 2015
Manually map an area-specific route
View RouteCollectionAreasExtensions.cs
public static class RouteCollectionAreasExtensions
public static Route MapAreaRoute(
this RouteCollection routes,
string areaName,
string routeName,
string url,
object defaults = null,
object constraints = null,
string[] namespaces = null)
sebnilsson / UriExtensions.cs
Last active Nov 16, 2017
Get relative part of an Uri
View UriExtensions.cs
public static string ToRelative(this Uri uri)
if (uri == null)
throw new ArgumentNullException(nameof(uri));
return uri.IsAbsoluteUri ? uri.PathAndQuery : uri.OriginalString;
sebnilsson / FakeHttpContextFactory.cs
Created Oct 24, 2014
Create a fake HttpContext, without using mocking-frameworks
View FakeHttpContextFactory.cs
public static HttpContext GetFakeHttpContext(
string url,
HttpRequest request = null,
HttpResponse response = null)
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
throw new ArgumentOutOfRangeException("url", "The URL must be a well-formed absolute URI.");
sebnilsson / SuppressXFrameOptionsHeaderAttribute.cs
Created Sep 30, 2014
View SuppressXFrameOptionsHeaderAttribute.cs
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class SuppressXFrameOptionsHeaderAttribute : ActionFilterAttribute
public override void OnResultExecuted(ResultExecutedContext filterContext)
filterContext.HttpContext.Response.Headers.Set("X-Frame-Options", "ALLOWALL");
sebnilsson / ToDynamicObjectExtensions.cs
Created Aug 25, 2014
Extension-method to convert any object to dynamic ExpandoObject
View ToDynamicObjectExtensions.cs
public static ExpandoObject ToDynamic(this object obj)
var expandoObject = new ExpandoObject();
if (obj == null)
return expandoObject;
var dictionaryValues = new RouteValueDictionary(obj);
if (!dictionaryValues.Any())