The ASP.NET MVC 3 default template includes an AccountController class, an AccountModels class, and a set of views in the Account folder. Together, these items allow for managing ASP.NET security including register, logon, and change password. Absent from the template, however, is an good method or example of how to use the returnUrl parameter that exists inside the Logon method of the Account controller:

public ActionResult LogOn(LogOnModel model, string returnUrl)

You can pass a URL (or route) to the parameter and the Logon method will do its job, returning the user to the page once the user has completed the logon. However, if you examine the default view generated for LogOn you will notice no mention or use of this parameter. Instead, just a standard Html.BeginForm() call.

A common scenario is to pickup the page that sent the user to the LogOn view and then route the user back to that page once they have completed a successful logon. You can do so with a few simple (and very effective) lines of code.

First, declare a string variable at the top of the LogOn view. Set this variable’s value to the Request object’s UrlReferrer.PathAndQuery property.  Make sure you check this value for null first as it would not be set in the case a user goes directly to the Logon page. The following code is an example (using the Razor syntax):

@model MvcApplication2.Models.LogOnModel



    ViewBag.Title = "Log On";


    string retUrl = "";

    if (ViewContext.HttpContext.Request.UrlReferrer != null)


      retUrl =



The only other step is to modify the BeginForm call on the same view page. Here you make sure to pass the string variable that is set the the referring URL. The following code shows an example:

@using (Html.BeginForm("Logon", "Account",
  new { model = this.Model, returnUrl = retUrl }))

That’s it! The LogOn action method in the AccountController class takes care of the actual redirect.

Hope this is useful to everyone.

I am writing some code using the new Entity Framework 4.1 code first model (where you simple objects to represent your database). I am using the System.ComponentModel.DataAnnotations to set rules such as required fields, validation with regular expressions, range validation, etc.

However, I could not find how you would implement the custom, “On[Property]Changing” patterns that is common in prior editions (using partial classes and partial methods). After a lot of looking, I stumbled upon the answer.

You use the new CustomValidation attribute. You can mark your property with this attribute as follows:

[CustomValidation(typeof(RegValidation), "ValidateAcceptTerms")]

public bool AcceptTerms { get; set; }


In this case, you are indicating that the validation code sits in the class, RegValidation inside the method, ValidateAcceptTerms.


You can then code this validation class as follows:

public class RegValidation


  public static ValidationResult ValidateAcceptTerms(bool isAccept)


    if (isAccept)


      return ValidationResult.Success;




      return new ValidationResult(

          "You must accept the terms of service in order to continue.");





You return an instance of ValidationResult (either success or with an error message) to indicate the results. In this way, you can implement custom validation on your EF 4.1 code first properties.

Note: since your objects are just simple objects, you can also write the logic in your setter and raise a validation exception. This will prevent the object from saving. However, it will not automatically get your validation error into the view. The CustomValidation attribute will.

Creating Custom Controls in ASP.NET

Posted on July 24, 2009 10:05 by mikesnell

I have a second sample chapter from my book, “Microsoft .NET Framework 3.5—ASP.NET Application Development”. This sample is the full version of chapter 10, “Creating Custom Web Controls”; you can download it by clicking the link at the end of this post.

I wrote this chapter based on what I believe developers need to succeed when writing most Web applications inside of ASP.NET. For this reason, I first focused on creating basic user controls. This is a principal skill required by ASP.NET developers to enable fast, straightforward code-reuse and consistency across their Web application.

This first lesson of the chapter is all about user controls. This includes managing events, working with properties, consuming controls, and even dynamically loading user controls at runtime. I also covered templated user controls for those times when you need a custom layout for your user control’s UI representation.

The second lesson is the more advanced topic of custom Web server controls. These controls provide re-use across applications. They can be compiled separately, installed, versioned, and shared . You can use the techniques in this lesson to create UI controls that are similar to those you buy or use inside of Visual Studio. This includes features like Toolbox support, custom designers, default properties, and more.

I hope you enjoy this sample chapter. Like all chapters in the book, it includes detailed labs and questions designed to help you study for the certification exam. You can get access to the lab source code and answers to the sample questions by purchasing the book.

9780735625624_CH10.pdf (804.47 kb)

ASP.NET AJAX and Client-Side Scripting

Posted on July 22, 2009 13:21 by mikesnell

I wanted to post a sample chapter from my book covering the MCTS Exam, 70-562: Microsoft .NET Framework 3.5—ASP.NET Application Development. You can download chapter 6, “Working with ASP.NET AJAX and Client-Side Scripting” using the link at the end of this post.

I wrote this chapter to help readers better understand the AJAX-related features that are now built into the .NET framework. This includes the AJAX extension controls such as partial-page update (UpdatePanel), progress indicator (UpdateProgress), and the timer control (Timer).

In lesson 2, I wanted to cover some Javascript basics and walk through the new Microsoft AJAX Library. This library is a wrapper that speeds your Javascript development by providing support for strong types, debugging, and more.

I hope you enjoy the sample chapter. Please feel free to send me feedback!

9780735625624_CH06.pdf (953.60 kb)