Last active
December 19, 2015 16:59
-
-
Save ssajous/5987594 to your computer and use it in GitHub Desktop.
Validation Filter to send a JSON response with validation errors when an HTTP Request contains an invalid data model. Uses JSON.net.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json.Serialization; | |
using System.Web.Http; | |
using System.Web.Http.Validation.Providers; | |
using MyApplication.Web.Filters; | |
namespace MyApplication.Web | |
{ | |
public static class CustomGlobalConfig | |
{ | |
public static void Customize(HttpConfiguration config) | |
{ | |
config.Filters.Add(new ValidationActionFilter()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Http; | |
using System.Web.Mvc; | |
using System.Web.Optimization; | |
using System.Web.Routing; | |
namespace MyApplication.Web | |
{ | |
public class MvcApplication : System.Web.HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
WebApiConfig.Register(GlobalConfiguration.Configuration); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
AuthConfig.RegisterAuth(); | |
CustomGlobalConfig.Customize(GlobalConfiguration.Configuration); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var showError = function(error) { // error is the JSON error message sent from the validation filter | |
if (error.responseText) { | |
var | |
msg = JSON.parse(error.responseText), | |
keys = _.keys(msg), | |
txt = ''; | |
_.each(keys, function(key) { | |
txt += msg[key] + ' '; | |
}); | |
displayErrorMessage(txt); // However this is defined for the system | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json.Linq; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http.Controllers; | |
using System.Web.Http.Filters; | |
namespace MyApplication.Web | |
{ | |
public class ValidationActionFilter : ActionFilterAttribute | |
{ | |
public override void OnActionExecuting(HttpActionContext context) | |
{ | |
var modelState = context.ModelState; | |
if (!modelState.IsValid) | |
{ | |
var errors = new JObject(); | |
foreach (var key in modelState.Keys) | |
{ | |
var state = modelState[key]; | |
if (state.Errors.Any()) | |
{ | |
errors[key] = state.Errors.First().ErrorMessage; | |
} | |
} | |
context.Response = context.Request. | |
CreateResponse<JObject>(HttpStatusCode.BadRequest, errors); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment