New Features Of .NET Core MVC

By releasing ASP.NET Core, Microsoft has joined a big community of free, open source, and platform independent software services. Instead of giving it just a new version number from the existing framework, Microsoft released a brand new tool because the company does not want people to get confused with this release as a further enhancement to the previous version of MVC,i.e., 5.0. So, just to clarify, Microsoft completely renamed it MVC Core.

The intent of this article is to quickly walk through the big changes Microsoft made in this release.

  • Platform Independent

  • All previous versions of .NET framework were restricted to a Windows only platform but to keep the market demand and changing needs, Microsoft revamped the .NET framework from scratch and named it .NET Core and ASP.NET Core. .NET Core not only supports Windows but it also supports Mac and Linux. Similarly, earlier, for development, we were mainly restricted to VS IDE but now VS Code can be used for Linux and Mac OS.

  • Fully Open Source

  • .NET Core and ASP.NET Core are released as open source projects. Their GitHub repositories are mentioned below.
    • https://github.com/dotnet/core
    • https://github.com/aspnet/Home
  • Using Kestrel (No more dependent on IIS only)

    When the things like platform independence come into the picture, then naturally, Microsoft needs to move up from IIS. IIS was the default web server till now to host ASP.NET applications. ASP.NET Core broke the framework dependency with IIS and gave way to host .NET web app with other web servers like Apache, IIS, NGnix.

    Kestrel is an open source, cross platform, light weight and a default webserver used for ASP.NET Core applications. By default, all Asp.Net core project templates include Kestrel webserver when we create new Asp.Net Core Projects. By default, all Asp.Net core project templates include Kestrel webserver when we create new Asp.Net Core Projects. Asp.Net Core applications use Kestrel webserver as an in-process server where the application will have same and consistent Startup process.

    (Source - Learn more about Kestrel )

  • Modular Framework

    .NET Core is a modular framework meaning, dot net core applications can be deployed as a standalone deployment rather than having a machine wide installation.

    Because of dependent on NuGet package, Developers are having complete flexibility to include only those components which are required for the application.

  • Middleware Components (No more HTTPModules and HTTPHandlers)

    In ASP.net framework we were having HTTPHandlers and HTTPModules, which is used to handle and process the web request. For ex- HTTPHandlers are responsible for what request is coming in and how to respond to the incoming request based on file extension or other parameters. This can be customized on the need basis and also can filter out the request based on the requested file extensions.

    In .NET Core, HTTPModules, and HTTPHandlers are no more. In its place, Middleware components are being introduced. The startup class in dot net core, configure() method can be set up to set the chain of middleware , which will execute and process as per the pipeline and will process, shortcircuit, or filter the request.

  • Command Line Interfaces

    Command line interfaces are available in dotnet core to handle almost everything. Like creating a project, building the project, running the project, restoring the package etc.

  • Multiple option for configuration

    Earlier dot net was having web.config files for configuration related items which the application has to read and use. Dot net core gives multiple options for configuration like JSON based configuration files, Environment variable, command line arguments etc.

  • Auto-compilation

    The moment C# file gets saved .Net compiler do the background compilation and browser refresh will automatically get the updated changes without explicit compilation.

  • Static files

    ASP.net core application uses a special folder named wwwroot. This folder is used to store static files like html, css, images, JS file etc. If the request comes to any of the static file, it can be processed without even involving MVC engine.

    Please note – We need to set up middleware to do static file processing, which will be part of separate blog.

  • RESTAPI Controller and MVC controller are same now

    Earlier WebAPI controller and MVC controller are part of different class now they are same. This means both the controllers now derive from Microsoft.AspNetCore.Mvc.Controller class

  • Tag Helpers

    Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files. Tag Helpers provides an HTML-friendly development experience and a rich IntelliSense environment for creating HTML and Razor markup.

    "Microsoft.AspNetCore.Razor.Tools" is the package that adds Tag Helper tooling.

    Earlier version of MVC, we were using HTMLHelpers. HTMLHelpers are invoked as methods with HTML in Razor markups.

    Ex – HTMLHelper –

     

    1. @Html.Label("EmployeeName""First Name:"new {@class="EName"})  

     

    @ - symbol tells the Razor, consider this line as a code line. It does not provide any intellisence.

    Now, let’s change it as Taghelper –

     

    1. <label class=”EName” asp-for=” EmployeeName”></label>  

     

    IntelliSense helps you to write the entire line. Asp-for, catches up all the properties and display as intellisense. Class name pulls up all the available options etc.

    Examples

    ASP-Action tag helper

     

    1. <a asp-controller="Account" asp-action="Register">Register</a>  

     

    HTML TagHelper in earlier verson of MVC

    1. @Html.ActionLink("Register""Register""Account")  
    2. <a href="@Url.Action("Register","Account")">Register</a>  

    Cache Tag Helper

    1. <cache expires-after="@TimeSpan.FromMinutes(10)">  
    2.   @Html.Partial("_WhatsNew") *last updated @DateTime.Now.ToLongTimeString()   
    3. </cache>  

    Image Tag Helper

    This tag helper helps to always pull the latest version of the image. By adding this Tag while downloading the image, web server adds a query string with it and when the image changes that means when the digital content of that image changes, server changes the query string and forced browser to pull latest image.

     

    1. <img src="~/images/logo.JPEG" asp-append-version="true" />  

     

  • View Components

    ASP.NET introduced View Components. View components are like Partial views with some significant improvements. View components depends upon the data which you passed to it, it does not depend upon model binding. Like Partial view, View components renders data in chunks. It also implements separation of concern, support independed testability and can also have parameters.

    View components itself is a very vast and powerful topic to discuss, which will be discussed separately.

  • Dependency Injection

    What is dependency injectio

    How the dependency injection is being used everywhere in ASP.net Core

    ASP.NET Core has a Dependency Injection container that we can use to define the dependencies that should be injected into controllers, views, or any other class that the framework will create for you. You can get started configuring the container with the ConfigureServices method in the Startup.cs file as below –

    Any class that adds a constructor with a parameter of type IConfigurationRoot will be provided the contents of the Configuration property in my Startup class. I can even enable Razor views to have dependencies injected by using the @inject directive.

    1. public Startup(IConfiguration configuration) {  
    2.     Configuration = configuration;  
    3. }  
    4. public IConfiguration Configuration {  
    5.     get;  
    6. }  
    7. // This method gets called by the runtime. Use this method to add services to the container.  
    8. public void ConfigureServices(IServiceCollection services) {  
    9.     services.AddMvc();  
    10. }  
    11. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
    12. public void Configure(IApplicationBuilder app, IHostingEnvironment env) {  
    13.     if (env.IsDevelopment()) {  
    14.         app.UseDeveloperExceptionPage();  
    15.         app.UseBrowserLink();  
    16.     } else {  
    17.         app.UseExceptionHandler("/Home/Error");  
    18.     }  
    19.     app.UseStaticFiles();  
    20.     app.UseMvc()  
    21. }  


Similar Articles