Skip to content

Instantly share code, notes, and snippets.

View janhebnes's full-sized avatar

Jan Hebnes janhebnes

View GitHub Profile
@janhebnes
janhebnes / gist:3962756
Created October 27, 2012 02:44
Sitecore CMS - PageNotFound 404 handling using a Sitecore Item as ErrorPage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Sitecore;
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Sitecore.Layouts;
using Sitecore.Pipelines.HttpRequest;
@janhebnes
janhebnes / Common.targets
Last active July 28, 2017 19:22
MSBuild - Just include in project file. Detects on project folder level latest TFS or Git Revision using tf.exe history or git rev-list count HEAD for a revision number and git log -1 pretty=format:%h . for hash and replaces the Revision in AssemblyVersion and AssemblyFileVersion for AssemblyInfo.cs prior to building the project with CustomTasks…
<!--
***********************************************************************************************
Embed in csproj with import and the build events are hooked up automatically
<Import Project="..\..\..\Common.targets" Condition="Exists('..\..\..\Common.targets')" />
PreBuild - Handling revision on build so the source control revision number of the project folder is embeded in the last numeric of the projects AssemblyInfoVersion and AssemblyInfoFileVersion
PostBuild - Sets revision to 0 and reverts the file with a copy upon finishing for not introducing change on version control for the assemblyInfo.cs
In debug mode the revision detection is disabled, and notification is shown instead.
@janhebnes
janhebnes / BatchCommands.cs
Last active September 15, 2016 09:00
Batching Commands for SQL and Optimizing network round trips. http://blog.jan.hebnes.dk/2016/09/batching-commands-for-sql-and_15.html
/// <summary>
/// Handle Database operations
/// </summary>
/// <remarks>The default network packet size is 4,096 bytes. More information at http://technet.microsoft.com/en-us/library/ms177437.aspx </remarks>
public class BatchCommands
{
private StringBuilder _query = new StringBuilder();
private int _bufferLength = 0;
public static string ConnectionString
{
@janhebnes
janhebnes / UnitTestIHttpActionResult.cs
Last active June 24, 2016 09:55
Sample Unit test looking at a Controller returning the WebAPI 2 pattern IHttpActionResult for reference.
[TestMethod]
public void Study_DatetimeOffsets_in_DayViewModel()
{
// http://www.asp.net/web-api/overview/testing-and-debugging/unit-testing-controllers-in-web-api
// The controller is returning IHttpActionResult, so look at the sample in the bottom of the ms article.
// Using OkNegotiatedContentResult safecast resulted in a null in my case.
var controller = new DayController();
controller.Request = new HttpRequestMessage();
controller.Configuration = new HttpConfiguration();
@janhebnes
janhebnes / DefaultDocumentsHttpModule.cs
Last active June 20, 2016 11:06
HttpHandler that helps respect the Request flow and serves the default documents of a directory instead of allowing e.g. Umbraco to kidnap the request for there purposes.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
/// <summary>
/// Respect the DefaultDocuments even when Umbraco takes over the HttpModule pipeline
/// Add file to App_Code (or compile with your project) and
/// Add to web.config on system.web > httpModules add name="MyDefaultDocumentsHttpModule" type="DefaultDocumentsHttpModule"
@janhebnes
janhebnes / gist:6463709
Last active December 22, 2015 11:09
Parsing Web Browser Languages can be a pain here are some code snippets we use for getting the actual browser languages
// Try to match the local date format of the browser
var userLanguages = LanguageItem.GetRequestUserLanguages();
// Ignore if no language preferences
if (userLanguages != null)
{
var culture = CultureInfo.CreateSpecificCulture(userLanguages.First().IsoCode);
return Updated.ToString("d", culture);
}
return Updated.ToString("dd.MM.yyyy");
@janhebnes
janhebnes / SitecoreNantWebService.asmx.cs
Last active December 19, 2015 20:38
Sitecore Webservice for creating a package and downloading it from webservice level. Part of the CruiseControl NANT Build Tasks that 1508 used back in 2010 on the phmetropol.dk project that won Sitecore Site of the Year two years in a row in the Educational category.
namespace FemtenNulOtte.NAnt.WebService
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;
@janhebnes
janhebnes / MailEncryption.cs
Created December 18, 2015 11:41
Allowing to sign an email with a private certificate and encrypting the email with a receivers public certificate key, taking care of handling potential issues arising when certificates are close to expiration or have expired
namespace FemtenNulOtte.Client.Logic.Utilities
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;
@janhebnes
janhebnes / SteamGroupRelationsProgram.cs
Created December 16, 2015 15:19
D3 visualization of Steam Group relations between friends and games using Steam API - see the results at http://netbyte.dk/steam/games/ and http://netbyte.dk/steam/
using System.IO;
namespace ConsoleApplication4_steamapi
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Policy;
using System.Text;
@janhebnes
janhebnes / FTPLocalSync.cmd
Last active December 16, 2015 15:05
WinSCP script for syncronizing remote FTP folder with local folder (example)
"C:\Program Files (x86)\WinSCP\winscp" /console /script="FTPLocalSync.winscp"