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 / 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>
@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 / 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 / _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 / 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 / HandleWebApiErrorAttribute.cs
Created January 8, 2015 21:26
Web API Error Attribute
// This attribute helps return exception text from web API methods when applied to a MVC Web API controller.
public class HandleWebApiErrorAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
var exception = context.Exception as Exception;
if (exception != null)
{
var message = exception.Message;
message = message.Replace('\r', ' ').Replace('\n', ' ');
@GeoffCox
GeoffCox / ScaleFontSizeToFit
Created March 9, 2015 22:30
Scale font-size until text fits within element
function scaleFontToFit(element) {
if (element) {
// for an element
var $element = $(element);
if ($element.css("overflow") == "hidden") {
var elementHeight = $element.height();
var fontSize = parseInt($element.css("font-size"), 10);
var $fullElement = $(element).clone().css('overflow', 'visible').height('auto');
@GeoffCox
GeoffCox / OverlayJSON.cs
Last active August 29, 2015 14:17
Overlay values over default values in a JSON object
private static object ApplyDefaultData(object data, object defaultData)
{
if (defaultData != null)
{
var target = new ExpandoObject();
// I apply the default values
string defaultJson = JsonConvert.SerializeObject(defaultData, _jsonSerializerSettings);
JsonConvert.PopulateObject(defaultJson, target, _jsonSerializerSettings);
@GeoffCox
GeoffCox / Global.asax.cs
Created April 16, 2015 00:07
Allow multiple origins for CORS using ASP.NET MVC
protected void Application_BeginRequest()
{
this.SetupCorsHeaders();
//OPTIONS request comes before the POST to know the permissions. this forces to send the reply.
if (((IList)Request.Headers.AllKeys).Contains("Origin") && Request.HttpMethod == "OPTIONS")
{
Response.Flush();
}
}
@GeoffCox
GeoffCox / CopyToClipboard.js
Created April 16, 2015 16:39
Copy HTML to clipboard in any browser
// It may be worth making a copy of the element within <body> that is hidden
// or even producing a section of items to copy.
// This way you can remove any interactivity styles that make the copied content not
// look good when pasted into other applications.
var element = document.getElementById('ClipboardTemp');
// This technique is simulating the user selecting these elements.
// Think of a ControlRange as a selection of controls and then executing the command like the user pressing 'Ctrl+C'
element.contentEditable = 'true';
var controlRange;