Sunday, January 26, 2014

Pass in URL line style variables to an ASP.NET Web API Controller.

Consider this blob of HTML and jQuery:

<input id="Name"/>
<br />
<input id="Email" />
<br />
<input id="Password" />
<br />
<input id="ConfirmPassword" />
<br />
<input id="SecurityQuestion" />
<br />
<input id="SecurityQuestionAnswer" />
<br />
<button id="CreatePerson">Submit</button>
<script type="text/javascript">
   $(function () {
   });
   $('#CreatePerson').bind('click', function () {
      $.ajax({
         type: "POST",
         url: '/api/Person?name=' + $('#Name').val() +
            '&email=' + $('#Email').val() +
            '&password=' + $('#Password').val() +
            '&confirmPassword=' + $('#ConfirmPassword').val() +
            '&securityQuestion=' + $('#SecurityQuestion').val() +
            '&securityQuestionAnswer=' + $('#SecurityQuestionAnswer').val(),
         dataType: 'json',
         success: function (result) {
            alert('Success!');
         }
      });
   });
</script>

 
 

What is above could feed into something that starts out like this on the C# side:

using System.Web.Http;
using FluffyNothing.Core.Objects;
namespace FluffyNothing.UserInterface.Controllers
{
   public class PersonController : ApiController
   {
      public SuccessOrFailure Post(string name, string email, string password, string
            confirmPassword, string securityQuestion, string securityQuestionAnswer)
      {

 
 

Addendum 1/27/2014: Something I am experimenting with here is trying to push all validations to C#. If password and confirmPassword do not match and we need to throw a message back to the user, and, well, let C# drive that sanity checking.

No comments:

Post a Comment