Skip to content

Instantly share code, notes, and snippets.

View darrelmiller's full-sized avatar

Darrel darrelmiller

View GitHub Profile
public class FileContent : HttpContent
{
private const int DefaultBufferSize = 1024 * 64;
private readonly string _fileName;
private readonly FileInfo _fileInfo;
public FileContent(string fileName, MediaTypeHeaderValue contentType = null)
{
Headers.ContentType = contentType ?? new MediaTypeHeaderValue("application/octet-stream");
public class ImageContent : HttpContent
{
private Image _image;
public ImageContent(Image image, MediaTypeHeaderValue mediatype)
{
_image = image;
Headers.ContentType = mediatype;
}
public class TokenValidationHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
const string errorMessage = "Unauthorized access";
const HttpStatusCode code = HttpStatusCode.Unauthorized;
var authValue = request.Headers.Authorization;
@darrelmiller
darrelmiller / gist:95d978bf0119c8723559
Created September 6, 2014 17:07
Specialized HttpContent class for CollectionJson
public class CollectionJsonContent : HttpContent
{
private readonly ReadDocument _readDocument;
private readonly JsonSerializer _serializer;
public CollectionJsonContent(Collection collection)
{
_serializer = JsonSerializer.Create(new JsonSerializerSettings
{
@darrelmiller
darrelmiller / gist:74158547016cecc31760
Created September 6, 2014 17:18
Alternate ways to use CollectionJson library
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Web.Http;
+using WebApiContrib.CollectionJson;
+using WebApiContrib.Formatting.CollectionJson;
+using WebApiContrib.Formatting.CollectionJson.Infrastructure;
+using WebApiContrib.Formatting.CollectionJson.Models;
+
@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
+{
public class CollectionJsonContent : HttpContent
{
private readonly ReadDocument _readDocument;
private readonly JsonSerializer _serializer;
public CollectionJsonContent(Collection collection)
{
_serializer = JsonSerializer.Create(new JsonSerializerSettings
{
@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);
@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: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)