Skip to content

Instantly share code, notes, and snippets.

View darrelmiller's full-sized avatar

Darrel darrelmiller

View GitHub Profile
@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: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
{
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;
public class ImageContent : HttpContent
{
private Image _image;
public ImageContent(Image image, MediaTypeHeaderValue mediatype)
{
_image = image;
Headers.ContentType = mediatype;
}
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");
@darrelmiller
darrelmiller / gist:d688282dfe8167fcc101
Created June 11, 2014 20:56
Embedded Resource Content
public class EmbeddedContent : HttpContent
{
private readonly Stream _Stream;
public EmbeddedContent(Type locatorType, string filename, MediaTypeHeaderValue contentType = null)
{
Headers.ContentType = contentType ?? new MediaTypeHeaderValue("application/octet-stream");
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" />
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="* | @* | node()" />
</xsl:copy>
</xsl:template>
class Startup
{
public void Configuration(IAppBuilder app)
{
var webApiconfig = new HttpConfiguration();
webApiconfig.Routes.MapHttpRoute("test","Test",new {controller="Test"});
app.UseWebApi(webApiconfig);
// Turn cross domain on
var config = new HubConfiguration { EnableCrossDomain = true };
public class JsonStringController : ApiController
{
public HttpResponseMessage Post([FromBody]string jsonBody)
{
var myobject = Newtonsoft.Json.JsonConvert.DeserializeObject<MyObject>(jsonBody)
return new HttpResponseMessage(HttpStatusCode.Created);
}
}
@darrelmiller
darrelmiller / gist:10682407
Created April 14, 2014 21:00
Create Owin host from HttpListener
public static IDisposable CreateHttpListenerServer(Uri baseAddress, Func<IDictionary<string, object>, Task> appFunc)
{
var props = new Dictionary<string, object>();
var address = Address.Create();
address.Host = baseAddress.Host;
address.Port = baseAddress.Port.ToString();
address.Scheme = baseAddress.Scheme;
address.Path = baseAddress.AbsolutePath;