Created
April 27, 2014 17:04
-
-
Save mauricedb/11350476 to your computer and use it in GitHub Desktop.
Validating data using ClearScript/V8 and JavaScript in the WebAPI
This file contains 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 (var engine = new V8ScriptEngine()) | |
{ | |
var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings; | |
var jsonBook = JsonConvert.SerializeObject(newBook, settings); | |
engine.Execute("var book = " + jsonBook); | |
var path = HttpContext.Current.Server.MapPath(@"~\App\ValidateBook.js"); | |
engine.Execute(File.ReadAllText(path)); | |
var result = engine.Evaluate("validateBook(book)") as string; | |
if (!string.IsNullOrEmpty(result)) | |
{ | |
return Request.CreateResponse(HttpStatusCode.NotAcceptable, result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment