ASP.NET MVC Making Web Development Fun Again

by Tyler Jensen 5. August 2008 01:03

I want to thank Scott Guthrie and Scott Hanselman and the whole ASP.NET MVC team for making web development fun again.

The simplicity of Rails, the power of .NET, and so much more. I've been reading the posts and watching some of the screencasts but had not yet tried it for real. That all changed over the weekend while working my latest pet project which I hope to reveal sometime soon. I decided to put MVC Preview 4 to the test. I was not disappointed.

I mean, who could not fall in love with the elegant simplicity of this view?

<tr>
  <td>Current password:</td>
  <td><%= Html.Password("currentPassword") %></td>
</
tr>
<
tr>
  <td>New password:</td>
  <td><%= Html.Password("newPassword") %></td>
</
tr>
<
tr>
  <td>Confirm new password:</td>
  <td><%= Html.Password("confirmPassword") %></td>
</
tr>
<
tr>
  <td></td>
  <td><input type="submit" value="Change Password" /></td>
</
tr>

Handled by this controller:

[Authorize]
public ActionResult ChangePassword(string currentPassword, string newPassword, string confirmPassword)
{

    ViewData["Title"] = "Change Password";
    ViewData["PasswordLength"] = Provider.MinRequiredPasswordLength;

    // Non-POST requests should just display the ChangePassword form
    if (Request.HttpMethod != "POST")
    {
        return View();
    }

    // Basic parameter validation
    List<string> errors = new List<string>();

    if (String.IsNullOrEmpty(currentPassword))
    {
        errors.Add("You must specify a current password.");
    }
    if (newPassword == null || newPassword.Length < Provider.MinRequiredPasswordLength)
    {
        errors.Add(String.Format(CultureInfo.InvariantCulture,
                    "You must specify a new password of {0} or more characters.",
                    Provider.MinRequiredPasswordLength));
    }
    if (!String.Equals(newPassword, confirmPassword, StringComparison.Ordinal))
    {
        errors.Add("The new password and confirmation password do not match.");
    }

    if (errors.Count == 0)
    {

        // Attempt to change password
        MembershipUser currentUser = Provider.GetUser(User.Identity.Name, true /* userIsOnline */);
        bool changeSuccessful = false;
        try
        {
            changeSuccessful = currentUser.ChangePassword(currentPassword, newPassword);
        }
        catch
        {
            // An exception is thrown if the new password does not meet the provider's requirements
        }

        if (changeSuccessful)
        {
            return RedirectToAction("ChangePasswordSuccess");
        }
        else
        {
            errors.Add("The current password is incorrect or the new password is invalid.");
        }
    }

    // If we got this far, something failed, redisplay form
    ViewData["errors"] = errors;
    return View();
}

Yes, you say, but what about all my high powered controls from XYZ Controls Company? Do they really make your life that much easier? Is HTML so hard? Have you looked at your ViewState lately? Yikes! Certainly there are tradeoffs, but you can bet that controls are on their way. Where Microsoft stack developers go, the control vendors soon follow.

Give me clean HTML with CSS style and controllers that support TDD in the most straightforward manner I've ever seen, sprinkle in some convention over configuration jazz from Ruby on Rails, and I'm a happy web developer again after having spent the last four and a half years avoiding ASP.NET while writing back end data and content analysis systems.

ASP.NET never looked better!

 

Tags:

Code

blog comments powered by Disqus

Me...

Tyler Jensen

Tyler Jensen
.NET Developer and Architect

Month List

Other Stuff