Skip to content

Instantly share code, notes, and snippets.

@frankgeerlings
frankgeerlings / gist:1711560
Created January 31, 2012 16:54
Remove trailing slash in URLs with ASP.NET MVC routing
// Put this in Global.asax.cs
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Do Not Allow URL to end in trailing slash
string url = HttpContext.Current.Request.Url.AbsolutePath;
if (string.IsNullOrEmpty(url)) return;
string lastChar = url[url.Length-1].ToString();
if (lastChar == "/" || lastChar == "\\")
@frankgeerlings
frankgeerlings / gist:2365821
Created April 12, 2012 09:28
Substitutable DateTime.Now
private static Func<DateTime> now = () => DateTime.Now;
public static Func<DateTime> Now
{
get
{
return now;
}
set
@frankgeerlings
frankgeerlings / gist:2377205
Created April 13, 2012 14:18
Stream.ReadToEnd -- to convert stream to byte array
using System;
public static class StreamExtensions
{
// http://stackoverflow.com/questions/1080442/how-to-convert-an-stream-into-a-byte-in-c
public static byte[] ReadToEnd(this System.IO.Stream stream)
{
var originalPosition = stream.Position;
stream.Position = 0;
@frankgeerlings
frankgeerlings / gist:2635991
Created May 8, 2012 15:02
ASP.NET MVC: SELECT with OPTGROUP from dictionary of select list items
public static class HtmlExtensions
{
public static IHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Dictionary<string, IEnumerable<SelectListItem>> selectList)
{
/*
* <select name="tmodel">
* <optgroup title="Items">
* <option value="item">Item</option>
* </select>
*/
@frankgeerlings
frankgeerlings / gist:2890188
Created June 7, 2012 17:25
SMTP server in Web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="someaddress@example.com">
<network
host="mail.example.com"
userName="something@example.com"
password="*************"
port="25" />
</smtp>
</mailSettings>
@frankgeerlings
frankgeerlings / Dutch.feature
Last active December 11, 2015 02:19
Use dutch (nl) Gherkin (for Cucumber) to describe features
#language: nl
Functionaliteit: Een nieuwe feature
Om domme fouten te voorkomen
Als wiskundemalloot
Wil ik de som van twee getallen optellen
Scenario: Optellen
Stel ik heb 50 op de rekenmachine ingevoerd
En ik heb 70 op de rekenmachine ingevoerd
Als ik op optellen druk
@frankgeerlings
frankgeerlings / ImageHttpHandler.cs
Created January 15, 2013 11:50
Handle images yourself (minimal implementation, add logic you might need here) — works on ASP.NET MVC as well as WebForms.
using System;
using System.Linq;
using System.Web;
public class ImageHttpHandler : IHttpHandler
{
21public bool IsReusable
{
get
{
@frankgeerlings
frankgeerlings / ActionDescriptorDescribesActionExtension.cs
Created February 12, 2013 10:58
Allows you to check what the current action is on action filters, e.g: if (filterContext.ActionDescriptor.DescribesAction<AccountController>(a => a.Login())) { Do(stuff); }
using System;
using System.Linq.Expressions;
using System.Web.Mvc;
public static class ActionDescriptorDescribesActionExtension
{
public static bool DescribesAction<TController>(this ActionDescriptor self, Expression<Func<TController, object>> action) where TController : IController
{
var actionName =
(action.Body is UnaryExpression
@frankgeerlings
frankgeerlings / CamelCaseDisplayNamesModelMetadataProvider.cs
Created October 10, 2013 10:54
Create useful names for fields instead of the camel cased property names. This one is slightly different in that it also capitalizes just the first word, which for some languages is more appropriate than having all the words' first letters as caps. Inspired largely by http://blog.dotsmart.net/2011/03/28/generating-better-default-displaynames-fro…
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Web.Mvc;
/// <summary>
/// Model metadata property names are typically CamelCased. When used as a human-readable text, eg. in LabelFor etc, this metadata provider
/// will un-camelcase the name of the property, so you don't have to add a DisplayNameAttribute to every single property in your model.
/// </summary>
@frankgeerlings
frankgeerlings / ExtendedApprovals.cs
Created November 15, 2013 14:10
Fix for iTextSharp created PDFs.
public class ExtendedApprovals
{
/// <summary>
/// This compares a PDF document saved in a byte stream.
/// </summary>
/// <param name="bytes">A byte array that represents the contents of the PDF file to be verified</param>
public static void VerifyPdf(byte[] bytes)
{
var actual = ScrubCreationDateInPdf(bytes);