Getting Started with ASP.NET Web API

In this sample we show how you can use ASP.NET Web API to build web APIs that support browser clients and can be easily called using jQuery.

C# (1.1 MB)
 
 
 
 
 
(7)
17,552 times
Add To Favorites
8/15/2012
E-mail Twitter del.icio.us Digg Facebook
Sign in to Ask a Question


  • Why you dont just call form.valid() instead of revalidate...
    2 Posts | Last Post April 17, 2013
    • Hi
      
      I was researching about web api validation and I found your article very interesting, My question is regarding custom validations. 
      
      I dont understand why you need the function: revalidate
      
      if change: (in post.js)
      
               400 /* BadRequest */: function (jqxhr) {
                   var validationResult = $.parseJSON(jqxhr.responseText);
                   form.valid();
               }
      
      the forms revalidate all the fields after the response from the webapi.
      
      Israel.
      
      
    • Hi again
      
      I will respond to myself :( and I am submitting a fix bug.
      
      Revalidate creates dynamically a new rule called: "failure" and the rule is attached to the element that dont fit validation rules performed on "server side"
      
      form.valid() does not works unless the new rule "failure" is attached to the element that fail validation (in the server).
      
      To attach the new rule to an element you just need to add new attribute: "data-val-[rulename]" so it is: "data-val-failure"
      
      So, that is what "Revalidate" does:
      
      - Attach to the failed element the attribute/rule: data-val-failure
      - Parse element (to apply the new rule)
      - form.valid() // Now launch validation after rules already attached.
      
      Now an interesting point, the current implementation shows a line:
      - $.removeData(form[0], 'validator');
      
      This line means (remove validator) from the form, a validator is like a class contains rules, messages, etc associated with the form. It is neccesary to remove it, because it will be recreated after every parseElement... If you dont removed, it just dont be re-created and the new rules will not take effect.
      
      The fix bug consist in the needed of apply 
      
      $.removeData(form[0], 'validator'); 
      
      before every parseElement, that means:
      
              for (var property in validationResult) {
                  $.removeData(form[0], 'validator');  // (*** fix here ***)
                  { ... }
                  jQuery.validator.unobtrusive.parseElement(item[0]);
              }
              form.valid();
              $.each(serverValidationErrors, function () {
                  $.removeData(form[0], 'validator');  // (*** fix here ***)
                  { ... }
                  jQuery.validator.unobtrusive.parseElement(item[0]);
              }
      
      The current implementation apply the "removal" only once (before enter the cicle), if you dont apply every time, you will activate only the first failed validator.
      
      Regards
      Israel.
      
      
  • Add Comment not working on VS 2012 Trial
    1 Posts | Last Post August 29, 2012
    • Well done for the sample, however I just installed VS 2012 trial on Windows 8 enterprise and Add comment throw object null exception. The comment object did not get initialized by the form values posted. Please let me know if there is a work around this.
  • Would this port to VS2010?
    1 Posts | Last Post July 27, 2012
    • This project would be very valuable if it was ported to vs2010. 
  • BackBone.js example
    1 Posts | Last Post July 08, 2012
    • Know any similar examples that use backbone.js? I don't know anyone that uses knockoutjs.
  • Description
    1 Posts | Last Post March 27, 2012
    • The description stuff within the zip file is for WP7.
  • Any samples against an Azure database
    3 Posts | Last Post March 01, 2012
    • Specifically, where queries are very specialized across multiple joins that cannot be handled by Entity Framework/REST calls, but need to fill an IEnumerable model with what is returned from the SQL Azure query.
      
      I still want to provide REST services for the CUD in CRUD however. I just need to create specialized queries that are not REST based alongside the REST services.
      
      Also, how would I allow only the browser to perform the special queries without authentication and the CUD operations require forms authentication?
      
      Do I just create a .svc page just for these special queries? Does this require special global.aspx/config settings?
    • For the authentication for CUD: You can use the [System.Web.Http.Authorize] attribute in actions inside ApiControllers, and that can require the authentication (which can be forms or some other mechanism) in the CUD methods, while leaving it out of the read one(s).
    • For the "special queries": you don't need a .svc file for that, you'd probably have a different controller (and use the routing to map to it), or another action in the same controller if you want to preserve the URI space - and you can pass the parameters in the query string, which aren't part of the route.