/ Published in: C#

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public class ValidateAntiSpamAttribute : ActionFilterAttribute { public string ErrorMessage { get; set; } public ValidateAntiSpamAttribute() { } public ValidateAntiSpamAttribute(string errorMessage) { this.ErrorMessage = errorMessage; } public override void OnActionExecuting(ActionExecutingContext filterContext) { string sixtimesnine = (string)filterContext.HttpContext.Request.Form["sixtimesnine"]; string fourtytwo = (string)filterContext.HttpContext.Request.Form["fourtytwo"]; if (string.IsNullOrEmpty(sixtimesnine) || string.IsNullOrEmpty(fourtytwo) || sixtimesnine != fourtytwo) { filterContext.Controller.ViewData.ModelState.AddModelError("Spam", this.ErrorMessage); } else { // Merely for unit testing puproses - we need to make sure that the above clause // was *not* run. filterContext.Controller.ViewData["filter"] = "passed"; } base.OnActionExecuting(filterContext); } }
Comments
