Cool features of ASP.NET MVC 4

I have been played around with MVC 4 for a while, now it is finally live.

I found there are quite a few great features included in MVC 4. Here I am going to chat about my favourite ones.

1. Extension to razor scripts

The default view engine supports, HTML attributes starting with ~/ will now be automatically replaced with the application root, developers no longer need to use the Url.Content function.

Another big time saver is conditional attributes. If the @styleClass variable in class="@styleClass" is null, the entirely attribute is automatically removed for you.

2. CSS & JS bundles

a. Instead of listing each css/js file needed by the client, I can use bundle to point to a specific folder.

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                 "~/Scripts/jquery-{version}.js"));

b. You can control the bundling and minification in web.config by turning compilation debug = true or false. Or you can override it from RegisterBundles() method

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                 "~/Scripts/jquery-{version}.js"));

    // Code removed for clarity.
    BundleTable.EnableOptimizations = true;
}

c. bundle is cached on the browser for one year. It sets the http header one year from when the bundle is created.

d. It supports LESS style sheet, by adding “dotless” NuGet package to your project.

implement the IBundleTransform interface,

using System.Web.Optimization;

public class LessTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        response.Content = dotless.Core.Less.Parse(response.Content);
        response.ContentType = "text/css";
    }
}

and create the bundle of LESS files and CssMinify transform in RegisterBundles() method in BundleConfig.cs file.

var lessBundle = new Bundle("~/My/Less").IncludeDirectory("~/My", "*.less");
lessBundle.Transforms.Add(new LessTransform());
lessBundle.Transforms.Add(new CssMinify());
bundles.Add(lessBundle);

Add it to the view,

 @Styles.Render("~/My/Less");

3. WebAPI

  • Allows the building of HTTP APIs that automatically can serialize to the format requested such as xml/json
  • Support oData so that if the returned object support iQueryable parameters can be added to the return to subsets (paging, top n etc)
  • Allows hosting of WebAPI either in an area web, console, service, windows 8
  • Allows results to easily return custom HTTP response codes and location header for querying data

4. Mobile view engine,

  • Supports adaptive rendering using media queries
  • Allows creation of different mobile specific views
  • Supports mobile templating engines such as jQuery mobile
  • 5. Real time communication

  • Uses SignalR.
  • .NET way of doing what everyone is using node.js for.
  • Allows the server to call JavaScript, e.g pushes a call to the browser thus executing some JavaScript
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s