Migrating from ASP.Net MVC 1 to ASP.Net MVC 2 : What should you know?
Even though Microsoft had been pushing ASP.Net for years it never had the cool, hi-tech image that some of the other frameworks enjoyed.
Serious programmers just didn't take ASP.Net seriously. To them that platform was dumbing down things too much, trying to make things too easy and hiding the true nature of the web. The ASP.Net controls and widgets created an abstraction layer which was too thick to penetrate and spewed markup that was clumsy, clunky and very often impossible to validate.
ASP.Net's page life-cycle was also a thorn for serious web-programmers. The event based structure and postback baggage made the webpages slower. Often too slow for creation of serious websites. No wonder then that ASP.Net wasn't compared favorably even with less robust technologies like PHP, and the new kids on the block like Ruby on Rails (RoR) stole thunderous applause.
That changed with the launch of MVC. Microsoft took a lot of ideas from RoR and created a new web-designing system that had none of the shortcomings of ASP.Net Classic and many of the advantages of .Net Framework's impressive capabilities.
MVC's popularity graph has been shooting up right since launch, and with the launch of the second version Microsoft has added some more new features to it. So if you've been using ASP.Net MVC or have been introduced to it, it's time to find out what's new in ASP.Net MVC 2, and how you can use it.
Areas
Early users of MVC complained that the system was too integrated and does not protect individual developers from each other. It also didn't make it easy for the team to work on different parts of the website without disturbing the rest of the code because the entire project shared the same set of Controllers and Models.
With the introduction of Areas this problem is solved. Consider an area like a protected sub-website inside the main project that has its own Controllers, Models, Views and Routes. This lets a team of programmers work on the same project, and write their code without worrying about making breaking changes to the main project. This will make it easier to create enterprise level projects by large teams.
Templated Helpers
Most of us have used the Helper methods in ASP.Net MVC 1. The helper methods could be used to abstract HTML rendering if you wanted. For some people it simplified creating the UI, but the helper methods weren't very smart when it came to understanding data and you had to cast most data to string to render it and if you had specific requirements you had to add markup too.
Templated Helper methods are a new addition to ASP.Net MVC 2 that let you create your own Helper method templates that can be used to work with data types and render the markup you want instead of that datatype.
Take DateTime for example. You can set up a helper method to show the date in special format instead of casting it everytime. Let's have a look at the code:
<%Html.DisplayFor(model => DateTime.Now); %>
The template method DisplayFor can be overridden to display the markup you want. All you need to do is to create a partial view in the Shared folder inside views. This view should be named DateTime.ascx (the type of the object to render). Here's the code inside it.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%=Model.HasValue == true ? Model.Value.ToShortDateString() : "No Date Specified" %>
Notice that in the page type declaration of the control we specify the type of the object (System.DateTime?). Just like we would declare a generic object.
Now the DisplayFor method would display the date in Short-date format.
Asynchronous Controller
It hasn't been said enough times that web is a synchronous medium. You queue a request, the wait for it to be processed and then queue another one. Then at the server end the requests are processed in a synchronous manner.
In ASP.Net requests are server by an application pool which assigns every incoming request to a thread. The thread then processes the request and returns the result. During the lifetime of the request the thread is busy and is added back to the pool when it's done.
This system works well in more cases but if the server gets too many requests it might consume all of the available threads and refuse any further requests with a Server Busy (503) response. Using Ascynchronous controllers you can overcome this problem by processing the requests in an asynchronous manner.
The thread hands over the processing to an asynchronous process and then is free to serve other requests. When the processing completes it might use a different thread than the first assigned to return the generated content.
This lets you server a lot more requests, but this is not a feature that everybody will use. Only high-traffic websites will find a use for this.
To implement asynchronous processing you must inherit the controller from AsyncController and implement Action methods in two parts Async, and Completed. Here's an example.
public void GetWebFileAsync()
{
AsyncManager.OutstandingOperations.Increment();
myAsyncTask myAsync = new myAsyncTask();
myAsync.SomethingDoneCompleted += (sender, e) =>
{
AsyncManager.Parameters["param"] = e.value;
AsyncManager.OutstandingOperations.Decrement();
};
}
public ActionResult GetWebFileCompleted(string[] param)
{
ViewData.Add("param", param);
return View("MyView");
}
The Asynchronous controller request is divided into two methods with suffixes: async and completed. Inside the async method you must call your asynchronous code. For each asynchronous call you need to increment the outstanding operations counter of the Asyncmanager and when that call is completed you need to decrement the Asyncmanager's outstanding operations counter.
The parameters passed to the asynchronous controller are also handled by the AsyncManager class in a parameters dictionary. When all the asynchronous operations are complete AsyncManager will call the completed method and there you can process your result and return the view.
The webpage address, redirecttoaction, and other requests will refer to your method name without the suffixes. Example our method would be called GetWebFile when we want to RedirectToAction to it.
RenderAction/Action
RenderAction/Action are new Helper methods in ASP.Net MVC 2 that solve an important problem, how to get the results of another Action from within an Action. RenderAction/Action solve this by processing the child-action and outputting the result either as string (Action), or directly to the response object (RenderAction). For practical purposes both are similar, though Action will let you retrieve the generated data as string and modify it further.
Here's a small example:
<%Html.Action("MySidebar") %>
This will render the Mysidebar action anywhere you place it.
Default Value in Action Methods
Another interesting little addition. Now you can have default values for parameters in action methods.
public ActionResult Index([DefaultValue(1)] int page)
{
ViewData.Add("page", page);
return View("Index");
}
If you didn't specify the value of the page variable in your url it would get set to a default of 1. Now you have two ways of supplying default values to parameters, either in the route (inside Globals.asax), or in the method body itself which sounds more intuitive.
More Validation Options
DataAnnotations
ASP.Net MVC 2 also expands the validation options you have. The new DataAnnotations validation attributes introduces RangeAttribute, RequiredAttribute, StringLengthAttribute and RegexAttribute that you can decorate your variables with.
public class MyData
{
[Required(ErrorMessage="The name is required")]
public string Name { get; set; }
[Range(1, 120, ErrorMessage="Invalid Age")]
public string Age { get; set; }
[RegularExpression(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$", ErrorMessage="Email address is not valid")]
public string Email { get; set; }
}
Client-Side Validation
Alongside DataAnnotations ASP.Net MVC 2 also has enhanced support for client-side validation. You can hook up the Model-validation to work with client-side validation using the validation javascript files that Microsoft provides in the ASP.Net MVC project itself.
HttpPost Attribute
Finally a very small addition. Till now we've been decorating the Post Action Methods using the AcceptVerbs(HttpVerbs.Post) attribute. ASP.Net MVC 2 introduces a shorter attribute -- the new HttpPost attribute. So now you can decorate your Post actions with the [HttpPost] attribute instead of AcceptVerbs.