Skip to content

Instantly share code, notes, and snippets.

View darrelmiller's full-sized avatar

Darrel darrelmiller

View GitHub Profile
public class TavisUriTemplateHandler : IUrlTemplateHandler
{
public IEnumerable<string> GetParameterNames(string template)
{
return new UriTemplate(template).GetParameterNames();
}
public Uri GetRequestUri(IUrlParameterFormatter urlParameterFormatter, RestMethodInfo restMethod, object[] paramList, string basePath = "")
{
string urlTarget = (basePath == "/" ? String.Empty : basePath) + restMethod.RelativePath;
{ "collection" :
{
"version" : "1.0",
"href" : "http://example.org/movies",
"items" : [
{
"href" : "http://example.org/movie/1",
"data" : [
{"name" : "Title", "value" : "Star Wars"},
{"name" : "Director", "value" : "Lucas"}
I can get this...
--6151f84b-b24a-41e0-a0ef-31398ab1d44f
Content-Disposition: form-data; name="files[image]"
--6151f84b-b24a-41e0-a0ef-31398ab1d44f--
With this,
// Wrapper Service
var customerService = new CustomerService();
var customer = customerService.GetCustomer(22);
application.Process(customer);
// Hypermedia Centric
var customerLink = linkFactory.Create<CustomerLink>();
customerLink.Id = 22;
application.FollowLink(customerLink);
@darrelmiller
darrelmiller / gist:972f01aa64c6e5df37df
Last active August 29, 2015 14:08
Check status of SqlServer
using System;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using Newtonsoft.Json.Linq;
@darrelmiller
darrelmiller / gist:52276dd06368039de197
Created October 29, 2014 15:24
A HtmlFormatter to allow conneg of HTML in MVC6
public class HtmlFormatter : OutputFormatter
{
public HtmlFormatter()
{
SupportedEncodings.Add(Encodings.UTF8EncodingWithoutBOM); // Whatever this should be
SupportedEncodings.Add(Encodings.UTF16EncodingLittleEndian); // Whatever this should be
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/html"));
}
public override bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
@darrelmiller
darrelmiller / gist:28221417620db2973a25
Created October 27, 2014 14:35
Coloured Request/Response console logger
public class ConsoleRequestLogger : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("> {0} {1}",request.Method,request.RequestUri.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped));
ProcessHeader(request.Headers, (name, value) => Console.WriteLine("> {0}: {1}", name, value));
if (request.Content != null)
{
@darrelmiller
darrelmiller / gist:0a267ff98aa911aa7c6b
Created October 21, 2014 16:10
HttpHeaders don't always preserve order
[Fact]
public void DoesNotPreserveHttpHeaderValueOrderOrCase()
{
var response = new HttpResponseMessage();
string input = "Private, max-age=60, s-maxage=60";
response.Headers.TryAddWithoutValidation("Cache-Control", input);
var header = response.Headers.First();
var output = string.Join(",", header.Value);
public class CollectionJsonContent : HttpContent
{
private readonly ReadDocument _readDocument;
private readonly JsonSerializer _serializer;
public CollectionJsonContent(Collection collection)
{
_serializer = JsonSerializer.Create(new JsonSerializerSettings
{
@darrelmiller
darrelmiller / gist:aee1faa6f58fedf65e09
Created September 6, 2014 17:18
More examples with CollectionJson
+using System;
+using System.Collections.Generic;
+using System.Web.Http;
+using WebApiContrib.CollectionJson;
+using WebApiContrib.Formatting.CollectionJson;
+using WebApiContrib.Formatting.CollectionJson.Infrastructure;
+using WebApiContrib.Formatting.CollectionJson.Models;
+
+namespace CollectionJson.Controllers
+{