Skip to content

Instantly share code, notes, and snippets.

@LSTANCZYK
LSTANCZYK / EnumModelValiation.cs
Created September 25, 2017 01:30 — forked from danielmackay/EnumModelValiation.cs
Web Api enumeration validation attribute. #webapi, #enum
using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
namespace Asset.Web.Filters
{
/// <summary>
/// Checks if an enumeration is valid. Will return true if no value is specified.
/// </summary>
public class EnumAttribute: ValidationAttribute
@LSTANCZYK
LSTANCZYK / Validation.cs
Created September 25, 2017 01:30 — forked from angelortiz-io/Validation.cs
Validate file extension ( .pdf in example ) in MVC project C#.
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Areas.Cms.Validation
{
public class ValidateExtensionAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
HttpPostedFileBase file = value as HttpPostedFileBase;
@LSTANCZYK
LSTANCZYK / RequiredUnlessSeqNum.cs
Created September 25, 2017 00:49
Custom Validator for dto_Address
using System.ComponentModel.DataAnnotations;
namespace Springsteen.WebApi.Contract
{
public class RequiredUnlessSeqNumAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
int seqNum;
@LSTANCZYK
LSTANCZYK / MustBeTrueAttribute
Created September 25, 2017 00:49 — forked from dejanvasic85/MustBeTrueAttribute
Custom validation attribute to ensure a checkbox is checked
/// <summary>
/// This is used on mostly checkboxes that ensure that they are ticked such as terms and conditions.
/// </summary>
public class MustBeTrueAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value == null) return false;
if (value.GetType() != typeof(bool)) return false;
public class PasswordsMustMatchAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(
object value, ValidationContext validationContext)
{
var model = validationContext.ObjectInstance as Person;
if (model.Password == model.PasswordConfirm)
{
return ValidationResult.Success;
}
@LSTANCZYK
LSTANCZYK / ValidateParametersAttribute.Test.cs
Last active September 25, 2017 00:47 — forked from gyuwon/ValidateParametersAttribute.cs
ValidateParametersAttribute.cs
using System;
using System.ComponentModel.DataAnnotations;
using DataValidation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CHR.API.Provisioning.ATT.Web.Tests
{
[TestClass]
public class OneOfValidationTests
{
@LSTANCZYK
LSTANCZYK / MustBeEmptyAttribute.cs
Created September 25, 2017 00:08 — forked from DaveHogan/MustBeEmptyAttribute.cs
MVC ValidationAttribute to ensure property is empty. Sounds strange but I create for an anti-bot field to which is hidden in css but bots still try filling in.
public class MustBeEmptyAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
if (value is string)
{
@LSTANCZYK
LSTANCZYK / ResourceNotFoundAttribute.cs
Created May 2, 2017 22:37 — forked from mikanyg/ResourceNotFoundAttribute.cs
WebApi ActionFilter used in conjunction with Service Fabric reverse proxy or ServiceFabric.AutoRest to indicate a RESTfull 404 response.
using System;
using System.Net;
using System.Web.Http.Filters;
namespace ServiceFabric.WebApi.Filters
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class ResourceNotFoundAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
// wrap ASF Stateless Service. Customer would implement this base class instead of StatelessService
public abstract class NServiceBusStatelessService : StatelessService, IProvideEndPointConfiguration
{
//plumbing for starting NSB on Service start
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
yield return new ServiceInstanceListener(
createCommunicationListener:
_ => new NServiceBusCommunicationsListener(this),
name:
@LSTANCZYK
LSTANCZYK / Firewall.cs
Created March 21, 2017 17:02 — forked from ChrisPelatari/Firewall.cs
ASP.NET MVC ActionFilter to allow or deny IPv4 addresses with optional subnet mask. For use with AppHarbor or any other server solution which uses the X-Forwarded-For header.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace AppHarbor.Web {
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true,
AllowMultiple = false)]