Skip to content

Instantly share code, notes, and snippets.

View tuespetre's full-sized avatar
😶

Derek Gray tuespetre

😶
View GitHub Profile
@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">
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 / 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
{
@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 / 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())