Skip to content

Instantly share code, notes, and snippets.

View DavidDeSloovere's full-sized avatar

David De Sloovere DavidDeSloovere

View GitHub Profile
@DavidDeSloovere
DavidDeSloovere / gist:1893621
Created February 23, 2012 16:33
Sample CacheAttribute for ASP.NET Web Api - i'm not saying this is the best way
namespace ProjectName.WebApi.Infrastructure
{
using System;
using System.Net;
using System.Net.Http.Headers;
using System.Web.Http.Filters;
public class CacheAttribute : ActionFilterAttribute
{
public CacheAttribute()
@DavidDeSloovere
DavidDeSloovere / CopyChmLocalAndOpen.ps1
Created January 4, 2013 08:30
Open up an old school .CHM help file from a network path. Put the Powershell in the folder of the .CHM file and any user can launch the help file via right-click on the .PS1 and selecting 'Run with Powershell'. (Set-ExecutionPolicy might need to be set first)
$tempfolder = $env:TEMP
$chmfile = Get-ChildItem | Where-Object {$_.Extension -match "chm"}
"Copying to local temp folder: $tempfolder"
Copy-Item $chmfile $tempfolder
"Opening $chmfile"
Start-Process $tempfolder\$chmfile
@DavidDeSloovere
DavidDeSloovere / JsonNetResult
Created June 1, 2013 09:39
JsonNetResult for ASP.NET MVC - correct formatting of dates and camel cased properties Got most of the code from Stack Overflow.
using System;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public class JsonNetResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
@DavidDeSloovere
DavidDeSloovere / gist:6277610
Last active December 21, 2015 08:19
System.Net.HttpResponseMessage set custom status code like 429
var response = new HttpResponseMessage
{
StatusCode = (HttpStatusCode)429,
ReasonPhrase = "Too Many Requests",
Content = new StringContent(string.Format(CultureInfo.InvariantCulture, "Rate limit reached. Reset in {0} seconds.", data.ResetSeconds))
};
response.Headers.Add("Retry-After", data.ResetSeconds.ToString(CultureInfo.InvariantCulture));
actionContext.Response = response;
@DavidDeSloovere
DavidDeSloovere / PrepResourceFilesForTranslator.bat
Created February 26, 2014 08:25
This ScriptCS code copies files into a folder while renaming them, and then compresses them into a zip file.
@ECHO OFF
CLS
ECHO ---
ECHO This command needs scriptcs (see http://scriptcs.net/)
ECHO If it is not installed you will get an error
ECHO ---
scriptcs PrepResourceFilesForTranslator.csx
ECHO ---
PAUSE
@DavidDeSloovere
DavidDeSloovere / AutoMapperConfiguration.cs
Created August 20, 2014 13:19
AutoMapperConfiguration Put AutoMapperConfiguration.Configure(); in global.asax
public static class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.CreateMap<OriginalEntity, DtoEntity>()
.ForMember(dest => dest.EntityId, source => source.MapFrom(s => s.Id));
}
}
@DavidDeSloovere
DavidDeSloovere / WebApiConfigRemoveXmlFormatter.cs
Last active August 29, 2015 14:05 — forked from beyond-code-github/WebApiConfig.cs
Remove the XML formatter so content is always returned as JSON.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var xmlFormatter = config.Formatters.XmlFormatter;
config.Formatters.Remove(xmlFormatter);
}
}
@DavidDeSloovere
DavidDeSloovere / WebApiConfigCamelCaseJson.cs
Created August 21, 2014 14:57
Camelcase JSON in Web Api
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// just camel case
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// or camel case plus nicer formatting
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
#####################
# BEGIN CONFIGURATION
#####################
Update-ExecutionPolicy Unrestricted
Set-WindowsExplorerOptions -DisableShowHiddenFilesFoldersDrives -DisableShowProtectedOSFiles -EnableShowFileExtensions -EnableShowFullPathInTitleBar
Disable-InternetExplorerESC
Disable-UAC
# Disable defrag (no need when having an SSD)
@DavidDeSloovere
DavidDeSloovere / web.config
Created September 3, 2014 07:32
Add caching for favicon in web.config
<location path="favicon.ico">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="90.00:00:00" />
</staticContent>
</system.webServer>
</location>