Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

ASP.NET Web API, Part 5: Custom Validation

0 (0 Likes / 0 Dislikes)
  • Embed Video

  • Embed normal player Copy to Clipboard
  • Embed a smaller player Copy to Clipboard
  • Advanced Embedding Options
  • Embed Video With Transcription

  • Embed with transcription beside video Copy to Clipboard
  • Embed with transcription below video Copy to Clipboard
  • Embed transcript

  • Embed transcript in:
    Copy to Clipboard
  • Invite a user to Dotsub
[Microsoft ] [] [Jon Galloway] Hi, this is Jon Galloway with Microsoft. We're continuing our look at web API with a more advanced scenario: Custom validation, and we're going to be hooking this up using an action filter. So, we're going to go into our model class and we're going to require text, author, and email, and we're doing this using a required attribute. This is a standard data annotation attribute. Then we're also going to require that the author is ten characters or less, and with all of these we want this to be set up so that this validation is happening on the server. So the way we'll enforce this is using a custom action filter attribute. You'll see we've created an action filter, and we start with validating the model state, so we take in that context as a parameter and validate it. If it fails, we want to build up information about what went wrong, so we're building up our collection of errors and we're doing this using the JSON object, which makes it very handy to build up dynamic information as a JSON object. Now it's great that we're packaging up all this error information, but the most important part is we need to return the right status code. Here you'll see we're setting the context response to include the right status code. We've got "http 400, bad request" being sent back, so that allows anyone to call into our service and immediately check that status code and find out whether or not their request succeeded. We've decided to register this globally, so we're going into our Global.asax to our configure method and we're calling config.Filters.Add, and then we're registering this validation filter, and that's going to apply to any web API service request. Now, anyone calling in to our services, they're going to get model validation, and they're going to get back both the correct status code— they'll get that 400 status code—and they will also get back the JSON data they will need to display it. Let's take a look at how this is being handled on the client. You'll see now we've got two status codes to check; 201 for created. If the request fails we'll get a 400—bad request—and we can then go in and dig into the JSON data that's returned to us and display it. You'll see in this case we're just using jQuery unobtrusive validation to show the validation failures. Now let's take a look at this in action. We're running the application. I'm going to try and submit a comment, and now my name is too long. It's greater than ten characters. You'll see when I post this back I'm going to get a bad request response, so that is an http 400, bad request. My service response also includes the right validation information in the JSON data, so when I look at my response body I've got everything I need to display the correct validation errors. When I shorten the name so that it passes validation, now you'll see I get that http 201, which is created, and the response message now includes the comment information about the comment I just created. That concludes our look at validation in web API. In the next screencast we'll take a look at authorization and authentication in web API. [Microsoft] []

Video Details

Duration: 3 minutes and 43 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 6
Posted by: neudesicasp on Sep 23, 2013

Note: This video contains information from a pre-release version of ASP.NET Web API. For information on what changed, see the updated sample code at

Caption and Translate

    Sign In/Register for Dotsub to translate this video.