Skip to content

Instantly share code, notes, and snippets.

View RhysC's full-sized avatar

Rhys Campbell RhysC

  • Perth; Australia
View GitHub Profile
@RhysC
RhysC / ObjectNotFoundExceptionFilterAttribute.cs
Created November 11, 2014 07:15
Error Handling 404 in Asp.Net MVC 5
using System.Web.Mvc;
namespace YourWebsite.Filters
{
public class ObjectNotFoundExceptionFilterAttribute : ActionFilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception is NHibernate.ObjectNotFoundException)
{
@RhysC
RhysC / CSharpObjectDefinition.cs
Created October 31, 2014 02:55
Code generator from JSON Schema - swap out the impl of ObjectDef to write, view model, dtos etc
using System;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Schema;
namespace JsonSchemaToCode
{
public class CSharpObjectDefinition : ObjectDefinition
{
public override string ToString()
@RhysC
RhysC / RedisRx.linq
Created October 21, 2014 12:02
LinqPad sample of Redis Pub sub using StackExchange.Redis and RX
<Query Kind="Program">
<NuGetReference>Newtonsoft.Json</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<NuGetReference>StackExchange.Redis</NuGetReference>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Reactive.Disposables</Namespace>
<Namespace>StackExchange.Redis</Namespace>
</Query>
void Main()
@RhysC
RhysC / LinqpadAsAWebserver.linq
Created October 17, 2014 04:17
Linqpad continues to be the coolest thing ever - WEBAPI in Linqpad with out all the drama. Logs all posts
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
<NuGetReference>Microsoft.AspNet.WebApi.OwinSelfHost</NuGetReference>
<Namespace>Microsoft.Owin.Hosting</Namespace>
<Namespace>Owin</Namespace>
<Namespace>System.Web.Http</Namespace>
<Namespace>System.Net.Http</Namespace>
<Namespace>System.Net</Namespace>
</Query>
@RhysC
RhysC / Log4NetAppenderFactory.cs
Created October 14, 2014 07:50
RestTest - sample console app for receiving HTTP Posts and logging said posts. Posting to http://localhost.fiddler:9000/api/test will allow you to see the request in fiddler
using System.IO;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
namespace RestTest.Logging
{
public static class Log4NetAppenderFactory
{
private static readonly PatternLayout PatternLayout = new PatternLayout("[%-5level][%date][%thread][%logger]: %message%newline");
@RhysC
RhysC / ClientStartUp.cs
Created September 24, 2014 10:09
Claims not coming back from identity server
using System.Collections.Generic;
using System.IdentityModel.Tokens;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using OauthScratch.MvcOwinClient;
using Owin;
@RhysC
RhysC / OwinSample.cs
Created September 12, 2014 06:02
OWIN sample midlewares in a console app
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using OwinSpike.Simple.Console;
//Console app
//NuGet packages :
//<package id="Microsoft.Owin.SelfHost" version="3.0.0" targetFramework="net45" />
@RhysC
RhysC / RequireApiKey
Created August 28, 2014 08:18
RequireApiKey require an api key for MVC controllers assumes SSL
public class RequireApiKey : ActionFilterAttribute
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(RequireApiKey));
public override void OnActionExecuting(HttpActionContext context)
{
var ipAddress = GetIpAddress(context);
Logger.InfoFormat("API attempt. Uri {0} - IP {1} - Headers {2} ", context.Request.RequestUri, ipAddress, context.Request.Headers);
IEnumerable<string> values;
if (context.Request.Headers.TryGetValues("ApiKey", out values) && GetApiKeys().Any (x => x ==values.First())
@RhysC
RhysC / CacheTests.cs
Last active August 29, 2015 14:05
Cache of T
using System;
using System.Threading;
using Xunit;
namespace MyNameSpace.Tests
{
public class CacheTests
{
[Fact]
public void CanReuseCachedObject()
@RhysC
RhysC / CmsController.cs
Last active August 29, 2015 14:04
Looking at a prisimic backed semi structure content delivery system...(TM) ;)
public class CmsController : BootstrapBaseController
{
private readonly PrismicProxy _prismicProxy;
public CmsController(PrismicProxy prismicProxy)
{
_prismicProxy = prismicProxy;
}
public ActionResult Dynamic(string id)