Bug Fixes and Changes in ASP.NET MVC 3 Beta
Update: This article is outdated. Please read this post for the latest new features.
Introduction:
Few days ago when ASP.NET MVC team released
ASP.NET MVC 3 Beta. This beta release includes some new
features, some changes, some improvements and bug fixes. For
detail of this beta release see
Announcing NuPack, ASP.NET MVC 3 Beta, and WebMatrix Beta
2. In this article, I will show you the most important
changes and bug fixes. Seeing and knowing new changes will
help you when you start using MVC 3 Beta from MVC 3 Preview
1 or MVC 2 because some properties and some classes are
renamed in MVC 3 Beta. On the other hand, seeing and
knowing bug fixes shows that how quickly ASP.NET MVC team
fixes the bugs discussed in forums, which is appreciable.
Description:
So let's see the latest changes and bug fixes in this release,
LayoutPage Property is Renamed:
In MVC 3 Preview 1 you can set the layout page using LayoutPage property of Razor views,
1 |
@{
|
2 |
LayoutPage =
"~/Views/Shared/_Layout.cshtml";
|
3 |
}
|
This property is renamed in MVC 3 Beta 1 to Layout,
1 |
@{
|
2 |
Layout =
"~/Views/Shared/_Layout.cshtml";
|
3 |
}
|
Razor View Engine Classes are Renamed:
In MVC 3 Preview 1, razor view and razor viewengine were named as CshtmlView and CshtmlViewEngine. In MVC 3 Beta, they are renamed to RazorView and RazorViewEngine. I think this makes much sense to rename Cshtml to Razor because it will clear the confusion.
Razor Views Namespaces are now Configurable:
In MVC 3 Preview 1, if you use a razor view then you can register global namespaces using CodeGeneratorSettings.AddGlobalImport method in Global.asax.cs,
1 |
CodeGeneratorSettings.AddGlobalImport("Namespace");
|
This class is removed in ASP.NET MVC 3 Beta because you can now register global namespaces via web.config. Just open the web.config inside Views folder of ASP.NET MVC 3 beta project, you will find the following configuration,
01 |
<system.web.webPages.razor>
|
02 |
<host
factoryType="System.Web.Mvc.MvcWebRazorHostFactory,
System.Web.Mvc, Version=3.0.0.0,
Culture=neutral,
PublicKeyToken=31BF3856AD364E35"
/>
|
03 |
<pages
pageBaseType="System.Web.Mvc.WebViewPage">
|
04 |
<namespaces>
|
05 |
<add
namespace="System.Web.Mvc"
/>
|
06 |
<add
namespace="System.Web.Mvc.Ajax"
/>
|
07 |
<add
namespace="System.Web.Mvc.Html"
/>
|
08 |
<add
namespace="System.Web.Routing"
/>
|
09 |
</namespaces>
|
10 |
</pages>
|
11 |
</system.web.webPages.razor>
|
Request Validation becomes More Granular:
ASP.NET helps in preventing Cross Site Scripting (XSS) (which is the most dangerous attack) via Request Validation. In previous versions of ASP.NET MVC, you can control request validation using ValidateInputAttriubte or globally control request validation by using a technique discussed in my past article. In the previous MVC versions, all the posted forms, headers, cookies and query string values are validated. In ASP.NET MVC 3 Beta, you can exclude some properties from request validation by introducing a new property Exclude on ValidateInputAttribute. For more details see this article.
Enhanced Dependency Injection Support:
In ASP.MVC 3 Beta, new services for dependency resolution has been added. For details see Brad(ASP.NET MVC team member) Dependency Injection Series here.
Unobtrusive Ajax and Unobtrusive Client Side Validation is Supported:
In my previous article, I talked about how you can move your client side script at bottom in here. In ASP.MVC 3 Beta, there is no need for doing this because now it separate the javascript behavior from the HTML by emitting HTML5 attributes. For details see Brad's this and this article.
Razor Improvements, Code Run Before, VBHTML Support, New Helpers:
In MVC 3 Beta, Razor syntax is enhanced, new helper methods has been added and vbhtml is supported. Also now you can execute some code before your views execute. For details see these release notes.
Views with Application Relative Path Problem is Fixed:
If you have the following action in MVC 3 preview 1 then it will throws an exception,
1 |
public
ActionResult Index()
|
2 |
{
|
3 |
return
View("~/Views/Home/Index.cshtml");
|
4 |
}
|
I explained this bug at here, This bug has been fixed in MVC 3 Beta but not 100% fixed. The following code still throws an exception in MVC 3 Beta. For details see this thread.
1 |
public
ActionResult Index()
|
2 |
{
|
3 |
return
View("Index", "~/Views/Shared/_Layout.cshtml");
|
4 |
}
|
Supports Client Side Validation for Overridden Properties :
In previous MVC versions, client side validation will not trigger for overridden model properties. For example, if you the following Model then client side validation will not trigger in old MVC versions but it will work in MVC 3 Beta. I explained the cause of this bug at here.
1 |
public
class
Human
|
2 |
{
|
3 |
public
virtual
int
Age { get; set; }
|
4 |
}
|
5 |
public
class
Men : Human
|
6 |
{
|
7 |
[Range(10,20,ErrorMessage = "Age is between 10 to 20.")]
|
8 |
public
override
int
Age { get; set; }
|
9 |
}
|
Support for DisplayAttribute :
In MVC 2 (and MVC 1), DisplayAttribute is not work because in .NET 3.5 there is no DisplayAttribute and MVC 2 is compiled with .NET 3.5. In MVC 3 preview 1, DisplayAttribute will be honored with the exception of template helpers. Now in MVC 3 Beta, EditorFor and DisplayFor helpers supports the ordering which is specified in the DisplayAttribute.Order property.
DisplayNameAttribute bug is Fixed:
In MVC 3 preview 1, usage of DisplayNameAttribute was broken. This has been fixed in MVC 3 Beta 1.
Page Directive bug is Fixed:
In MVC 3 preview 1, if you use any directive after the Page directive(<%@Page) then your page will break. This was a bug in MVC 3 preview 1 which shows on any page where there are any directives after the page directive. This has been fixed in MVC 3 Beta 1.
Model Property Name bug is Fixed:
In MVC 2 if you have a Model which contains a property named Model and you use it in a HTML helper then it will throws an exception. For example,
1 |
public
class
MyModel
|
2 |
{
|
3 |
public
string
Model { get; set; }
|
4 |
}
|
1 |
<%=Html.TextAreaFor(m =>
m.Model)%>
|
This will throw an exception in MVC 2 while the same sample is work in MVC 3.
Client Side Validation Support for StringLengthAttribute MinimumLength Property:
Client side validation is not supported for StringLengthAttribute MinimumLength property in MVC 2 In MVC 3 client side validation will work for MinimumLength property.
Summary:
In
this article I showed you the changes, new features and bug
fixes in ASP.NET MVC 3 Beta. This will help a lot when you
start using ASP.NET MVC 3 Beta. I also showed you how
quickly ASP.NET MVC team fixes the bugs discussed in forums.
If you have any questions, suggestions, issues or if you
found any bugs when working with this release, please post
them to the
ASP.NET MVC forum, where ASP.NET community members are frequently able to
provide informal support. You can also download the MVC 3
Beta source at
here. Hopefully you will enjoy this article too.