Skip to content

Instantly share code, notes, and snippets.

View GeoffCox's full-sized avatar
🏠
Working from home

Geoff Cox GeoffCox

🏠
Working from home
View GitHub Profile
@GeoffCox
GeoffCox / Global.asax.cs
Created January 5, 2015 19:08
Use camel case for MVC web-API
private static void SetJsonSerializerDefaults()
{
// I set the default JSON setting to provide camel-cased properties
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
// I set the web API serializer for JSON to use JsonConverts default settings
@GeoffCox
GeoffCox / _getErrorMessage.ts
Created December 18, 2014 17:50
Extract ajax error message from MVC exception
private _getErrorMessage(jqXHR: JQueryXHR, textStatus, errorThrown): string {
var message = textStatus + ' ' + errorThrown;
var error = <any>$.parseJSON(jqXHR.responseText);
if (error) {
message = error.Message;
if (error.ExceptionMessage) {
message += error.ExceptionMessage;
}
}
@GeoffCox
GeoffCox / CastAs.cs
Last active August 29, 2015 14:09
Stop C# compiler from static type-checking
// Sometimes the compiler will try to statically type-check when you don't want it to.
// TODO: I need to get a better example in here.
// This extension method to object makes the static type checker give up through the use of a generic method that does the casting.
public static T CastAs<T>(this object value)
{
return (T)value;
}
@GeoffCox
GeoffCox / GitHubRestCall.cs
Last active December 10, 2020 23:24
Call GitHub REST API using HttpClient
WebRequestHandler handler = new WebRequestHandler()
{
UseProxy = true,
};
var client = new HttpClient(handler);
client.BaseAddress = new Uri("https://api.github.com/");
// You should set the version so that GitHub knows what API you area calling
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
@GeoffCox
GeoffCox / ProjectModsForTypescript.xml
Last active August 29, 2015 14:08
Update Visual Studio 2012 Project File to support TypeScript
// Note: Install WebEssentials for VisualStudio 2012
// Add the TypeScript properties to the debug & release compile groups
// For example: <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptRemoveComments>false</TypeScriptRemoveComments>
<TypeScriptSourceMap>true</TypeScriptSourceMap>
<TypeScriptModuleKind>AMD</TypeScriptModuleKind>