Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
christopherbauer / WebRequest.cs
Created August 18, 2015 20:23
Webrequest to web api (oh god why)
public static string PostImage(byte[] imageBytes)
{
string output;
var boundary = GetBoundary(); //boundaries are placed between every form value and file in a MIME message
var boundaryFormatted = string.Format("--{0}\r\n\r\n", boundary);
var boundaryBytes = Encoding.ASCII.GetBytes(boundaryFormatted); //this is ascii, but later on everything is encoded in UTF8. Why, you ask? I do not know.
@christopherbauer
christopherbauer / DataTableController.cs
Created August 24, 2015 20:59
DataTables Api Stuff
public class DataTablesController : ApiController
{
private readonly ICustomerRepository _customerRepository;
public DataTablesController(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
public DataTableResult Get([FromUri]DataTableParameters parameters)
public class DataTableParameters
{
/// <summary>
/// Draw counter which is used by data tables to ensure ajax requests are drawn in sequence
/// </summary>
public int draw { get; set; }
/// <summary>
/// Paging first record indicator, the start point of the current data
/// </summary>
public class DataTableResult
{
/// <summary>
/// Response to the draw from DataTablesParameters, always cast the parameter value to int before returning it to prevent XSS
/// </summary>
public int draw { get; set; }
/// <summary>
/// Total records prior to filtering (in DB)
/// </summary>
using System;
using System.Linq;
using System.Web.Http;
using Customer.Models;
namespace Customer.api
{
public class DataTablesController : ApiController
{
private readonly ICustomerRepository _customerRepository;
[TestFixture]
public class when_getting_employee
{
[Test]
public void then_should_return_ok_result_given_employee_id()
{
// Arrange
var controller = new EmployeeController(new Mock<IEmployeeRepository>().Object);
// Act
function disableChecker(checkSelector, disableValue, targetSelector) {
var optionValue = $(checkSelector).val();
if (optionValue === disableValue) {
$(targetSelector).prop("disabled", true); //Disable target
} else {
$(targetSelector).prop("disabled", false); //Enable target
}
}
public class RequiredIfAttributeTests
{
[TestFixture]
public class when_validating_required_if_attribute
{
private class TestModel
{
[RequiredIf("Property2", true)]
public int? Property1 { get; set; }
public bool Property2 { get; set; }
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
public class AtLeastOneTrueAttribute : ValidationAttribute
{
private readonly string _targetProperty;
public AtLeastOneTrueAttribute(string targetProperty)
{
_targetProperty = targetProperty;
}
public class Zipcode : ApiController
{
private readonly ZipcodeRepository zipcodeRepository;
public Zipcode(ZipcodeRepository zipcodeRepository)
{
this.zipcodeRepository = zipcodeRepository;
}
public OkNegotiatedContentResult<string> Get(string zipcode, string state)