Skip to content

Instantly share code, notes, and snippets.

@GuyHarwood
GuyHarwood / PostFormAjax.js
Last active December 11, 2015 21:58
post a form using ajax
$(document).ready(function () {
$('#createEvent').click(function () {
$('#feedback').removeClass().html('').fadeOut('slow'); //optional, but nice
var form = $('#createForm');
$.ajax({
type: "POST",
url: $(form).attr('action'),
data: $(form).serialize(),
error: function () {
@GuyHarwood
GuyHarwood / IntToFlagsArray.cs
Last active December 11, 2015 22:49
Convert an int into an array of its respective enum flags
//the flags
[Flags]
public enum Role
{
None = 0x0,
User = 0x1,
Administrator = 0x2,
SysAdmin = 0x4
}
@GuyHarwood
GuyHarwood / FileUploadHttpPost.cs
Last active December 12, 2015 05:28
Accept form POST with file upload
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload(int id, IEnumerable<HttpPostedFileBase> files)
{
var results = new List<UploadResult>();
var uploads = new List<PhotoUpload>();
foreach (var file in files)
{
if (file.ContentLength > 0)
@GuyHarwood
GuyHarwood / SetActiveTabInBootstrap.js
Created February 12, 2013 16:47
When using Bootstrap tabs in ASP.Net MVC, this allows you to find the anchor tag with the href that matches the current page via Request.RawUrl
$('a[href$="@Request.RawUrl.Replace("/",@"\/")"]').parent().addClass('active');
public static bool IsNull<T>(this T instanceToCheck) where T : class
{
return instanceToCheck == default(T);
}
@GuyHarwood
GuyHarwood / stereotypicalLayers.cs
Last active December 14, 2015 04:39
I am sick of seeing code like this...
//the 'domain/service' layer...
public class ContactService
{
public ContactService(IContactRepository contactRepository)
{
_contactRepository = contactRepository;
}
public Contact GetContact(int id)
{
@GuyHarwood
GuyHarwood / AppPoolPerms.bat
Created February 26, 2013 12:33
On Windows Server 2008 v1 the GUI does not pick up the APPPOOL prefix for accounts. You need to use the command line to assign permissions for an application pool
ICACLS C:\inetpub\wwwroot\MyWebsite /grant "IIS APPPOOL\MyAppPool":(OI)(CI)(RX)
kernel.Bind(x => x.FromAssembliesMatching("Company.Product.Namespace.*")
.SelectAllClasses()
.BindDefaultInterface()
.Configure(b => b.InRequestScope()));
@GuyHarwood
GuyHarwood / EFDataContextTest.cs
Last active December 15, 2015 14:58
Base Test class with Lazy Entity Framework Context and database Initialisation.
using System;
using System.Data.Entity;
using System.Diagnostics;
using NUnit.Framework;
namespace EntityFrameworkTests
{
//TODO replace this stub with your own
public class MyDataContext : DbContext
{}
@GuyHarwood
GuyHarwood / BaseApiControllerTests.cs
Last active December 16, 2015 13:29
Scan all API controllers to ensure that they all inherit from the specified base controller
[Test]
public void AllApiControllersInProjectInheritFromBaseApiController()
{
var apiBaseType = typeof(BaseApiController);
var controllerTypes = (from type in apiBaseType.Assembly.GetTypes()
where type.Name.EndsWith("Controller") && type.IsSubclassOf(typeof(ApiController))
&& !type.IsSubclassOf(typeof (BaseApiController))
&& type != typeof(BaseApiController)
select type.Name).ToArray();