/
RequiredHttpAttribute.cs
67 lines (62 loc) · 2.85 KB
/
RequiredHttpAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Web;
namespace DataAnnotationsExtensions.ServerSideValidators
{
/// <summary>
/// RequiredHttpAttribute class. Identical behavior to the RequiredAttribute class
/// with the additon that it allows for bypassing validation based on the Http
/// method of the current Http request.
/// </summary>
/// <remarks>
/// This class will have identical behavior to the RequiredAttribute class in the scenario
/// where the System.Web.HttpContext.Current property returns null.
/// </remarks>
[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class RequiredHttpAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
/// <summary>
/// Gets or sets the HTTP methods that this attribute will validate on.
/// </summary>
/// <remarks>The value is treated as a regular expression pattern, e.g., GET|POST.</remarks>
public string HttpMethod { get; set; }
/// <summary>
/// Validates the specified value.
/// </summary>
/// <param name="value">The value to validate.</param>
/// <returns>True if the value is valid and false otherwise.</returns>
public override bool IsValid(object value)
{
var required = this.RequiredInContext(HttpContext.Current);
return required ? base.IsValid(value) : true;
}
/// <summary>
/// Validates the specified value with respect to the current validation attribute.
/// </summary>
/// <param name="value">The value to validate.</param>
/// <param name="validationContext">The context information about the validation operation.</param>
/// <returns>An instance of the ValidationResult class.</returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var required = this.RequiredInContext(HttpContext.Current);
return required ? base.IsValid(value, validationContext) : ValidationResult.Success;
}
/// <summary>
/// Determines if the provided HTTP context permits validation.
/// </summary>
/// <param name="context">The HTTP context.</param>
/// <returns>True if validation is required and false otherwise.</returns>
protected bool RequiredInContext(HttpContext context)
{
if (context != null && !string.IsNullOrWhiteSpace(this.HttpMethod))
{
if (!Regex.IsMatch(context.Request.HttpMethod, this.HttpMethod, RegexOptions.IgnoreCase))
{
return false;
}
}
return true;
}
}
}