Part II

In this blog post, I have listed a few more points to complete our previous write up on the same topic. I repeat, this is an opinion based list and if other changes come to your mind, please share with us in the below comment section.

5. View Components

It’s time to say hello to View Components!
In previous versions of ASP.NET MVC, one used the Html.Action() helper to call on a subcontroller. For instance, imagine that you want to exhibit banner ads in multiple views. In that case, you would develop a subcontroller that contained the logic for returning a specific banner advertisement and call the subcontroller by invoking Html.Action() from a view.

Subcontrollers – the Html.Action() helper — are not present in the current beta of MVC 6. Instead, MVC 6 includes an alternative technology called View Components.

Here’s how you can develop a View Component that displays one of two banner advertisements depending on the duration of day:

using Microsoft.AspNet.Mvc;

using System;

namespace Partials.Components

{

public class BannerAd : ViewComponent

{

public IViewComponentResult Invoke()

{

var adText = “Buy more coffee!”;

if (DateTime.Now.Hour > 18)

{

adText = “Buy more warm milk!”;

}

return View(“_Advertisement”, adText);

}

}

}

If the time is before 6:00pm then the View Component displays a partial named _Advertisement with the advertisement text “Buy more coffee!”. If the time is after 6:00pm then the text changes to “Buy more warm milk!”.

Here’s what the _Advertisement partial looks like:

@model string

<div style=”border:2px solid green;padding:15px”>

@Model

</div>

Finally, here is how you can use the BannerAd View Component in MVC view:

@Component.Invoke(“BannerAd”)

View Components are very similar to subcontrollers. But, subcontrollers were always a little odd.

6. GruntJS, NPM, and Bower Support

Front-end development gets a lot of appriciation in ASP.NET 5 via its support for GruntJS (and eventually Gulp).

GruntJS is a task runner that helps you to build front-end resources such as JavaScript and CSS files.

In order to support GruntJS, Microsoft needed supporting 2 new package managers (beyond NuGet). 1st, because GruntJS plugins are distributed as NPM packages, Microsoft added support for NPM packages.

2nd, as many client-side resources – such as jQuery, Twitter Bootstrap, Polymer, and AngularJS – are distributed via Bower, Microsoft added support for Bower.

This points at that you can run GruntJS use plugins from NPM and client resources from Bower.

7. Unified MVC and Web API Controllers

In previous versions of ASP.NET MVC, MVC controllers have been different than Web API controllers. An MVC controller utilised the System.Web.MVC.Controller base class and a Web API controller used the System.Web.Http.ApiController base class.

But in MVC 6, there is one and only one Controller class that is the basic class for both MVC and Web API controllers. There is only the Microsoft.AspNet.Mvc.Controller class.

MVC 6 controllers return an IActionResult. When utilised as an MVC controller, the IActionResult could be a view. When used as a Web API controller, the IActionResult may be data. The same controller might have actions that return both views and data.

In MVC 6, both Web API controllers and MVC controllers make use of the same paths. You can make use of either traditional-based routes or attribute routes and they apply to all controllers in the project.

8. AngularJS

This is one of the most popular client-side frameworks for building Single Page Applications (SPAs). VS 2015 has templates for developing AngularJS modules, controllers, directives, and factories.

There is a support for GruntJS in ASP.NET 5, which makes ASP.NET an excellent server-side framework for building client-side AngularJS apps. You can combine and compress all of your AngularJS files automatically whenever you perform a build.

WE conclude here.

If you want to improve your skill in ASP.Net and excel yourself in ASP.NET training program; our institute, CRB Tech Solutions would be of great help and support for you. Come join us with our scientifically designed program of .Net.


Stay connected to
CRB Tech for more technical optimization and other updates and informations.