Skip to content

Instantly share code, notes, and snippets.

View tuespetre's full-sized avatar
😶

Derek Gray tuespetre

😶
View GitHub Profile
@tuespetre
tuespetre / gist:f333b6bba4f9a8b496bc
Created October 11, 2014 15:17
Postal.Mvc5 HttpContext workaround (issue #73)
// Workaround for Postal.MVC5 1.2.0 issue 73
// https://github.com/andrewdavey/postal/issues/73
if (HttpContext.Current == null)
{
var baseURL = ConfigurationManager.AppSettings["baseURL"].TrimEnd('/');
var absolute = VirtualPathUtility.ToAbsolute("~/").TrimStart('/');
HttpContext.Current = new HttpContext(
new HttpRequest("", baseURL + "/" + absolute, ""),
new HttpResponse(new StringWriter())
@tuespetre
tuespetre / MultipartFormDataValueProvider.cs
Last active August 29, 2015 14:19
For use with pre-MVC6 Web API controllers
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.ValueProviders;
public class MultipartFormDataValueProvider : IValueProvider
{
@tuespetre
tuespetre / VersionedStaticFileHelper.cs
Last active August 29, 2015 14:22
An MVC helper class for adding hashes to versioned files (css, js, images, etc.)
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace Your.Project.Helpers
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.WebPages;
public static class IsomorphicRazorExtensions
{
public static HelperResult Foreach<T>(this HtmlHelper helper, IEnumerable<T> items, Func<T, HelperResult> template)
where T : class, new()
@tuespetre
tuespetre / README.md
Last active March 2, 2016 14:53
A snippet to use for AJAX; robustness in the face of 401 and other errors

Handles 401 errors for OAuth users following the 'code flow' (redirects) and notifies the user if there is a fatal error. You wouldn't want the server giving your users 401s outright though -- only when the request is AJAX (X-Requested-With: XMLHttpRequest.)

Uses jQuery (obviously) as well as Bootstrap modals. I'm not using PJAX just yet but I will include it when I do.

In the following example, I've placed the following elements as the last children of the <body> element. Note the addition of the empty data-remote attribute on #dynamic-modal -- this prevents Bootstrap from performing a redundant load the first time the modal is used.

<div class="modal fade special" id="error-modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
@tuespetre
tuespetre / Extensions.Tasks.cs
Last active June 27, 2016 16:44
Extensions for background tasks inside of a .NET core web app
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Builder
{
public static class WebApplicationBackgroundTaskExtensions
@tuespetre
tuespetre / powershell-setup.md
Last active November 10, 2016 13:25
Setting up powershell properly :P
  1. Install Ps-Get:

(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex

  1. Install posh-git:

Install-Module posh-git

  1. Edit or create %userprofile%/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1:
@tuespetre
tuespetre / repro.html
Created December 7, 2016 17:56
Repro of a WebKit bug with iOS 10 Safari
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<style>
.hover-me:hover {
color: red;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>inline-or-nah</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@tuespetre
tuespetre / FilterHelper.cs
Last active June 29, 2017 08:06
A utility class and general pattern for creating Github Issues-style Elasticsearch query string builder UIs.
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Your.Web.Project
{
public static class FilterHelper
{
public static bool HasOption(string query, string option)
{
var regex = GetRegex(option);