Learning never exhausts the mind
Home >  Coding > C# ASP.Net MVC > What’s New in MVC6 and Why MVC6 is the Best Yet

Published 8th March 2016 by

There are lots of changes introduced in MVC6, not least of which is a new view engine to replace Razor. This tutorial outlines the changes to MVC6 and explains why this is the best version yet.

In the latest ASP.Net 5, web forms have been removed. Instead, ASP.Net 5 uses only the MVC6 model. Because of this, there are several large changes to MVC6. In this article, we will look at controllers, tag helpers, Web API and the new view components.

MVC6 and WebAPI

MVC is the web framework for user interfaces, while WebAPI is the framework for REST web services. WebAPI was created using the exact same programming model that MVC uses. As such there are a lot of duplication between them.

MVCWeb Api
Model BindingModel Binding

They were originally created separately as MVC is tied to System.Web assembly, while WebAPI does not use it. System.Web is tied directly to the Microsoft IIS web server, and thus targeting different servers or operating systems is tricky. Because WebAPI is not tied to IIS, applications benefit from greater performance and the possibility to be self-hosted.

In .Net Core, MVC 6 has been rewritten and does not require System.Web. Now, because neither MVC or WebAPI use System.Web, they two frameworks can be unified. There is now one framework, MVC6 which does both GUI and API.

ASP.Net 5 and MVC6 also use dynamic in-memory compilation which means that you do not have to rebuild entire applications to see changes in the browser. Instead, simply save the changes to the controller or view, switch to the browser and hit refresh. Cool!

MVC6 Tag Helpers

Layout pages remain the same, however the Razor syntax has been extended to include an HTML friendly tag based system. You can still use @{ } syntax, but for rendering tags Tag Helpers are used.

In previous versions, you may have used something similar to the below to render a text box.

@Html.EditorFor(m => m.Name)

In MVC6 however, a much more friendly input tag is used.

<input asp-for="Name" />

There are several benefits to this. Firstly, the view is semantic HTML and can be viewed in a browser without running through the ASP.Net pipeline. This allows front end developers to work on the file without compiling the app all the time. It also allows HTML from designers to be used in the app much more quickly, since only a tag needs to be added, rather than converting to @Html. Syntax. The page can also be passed back and forth between developer and designer and the tags do not break the designer's application.

There are lots of tag helpers built in, and you can even add your own by deriving from the base tag helper class. In the views folder, create a new view called _ViewStart.cshtml. In here you include the tags that will be used in all views. All the built-in tag helpers are in the Microsoft.AspNet.Mvc.TagHelpers assembly. You can add this to each page as well, or specific tags on certain pages.

@addTagHelper "*,Microsoft.AspNet.Mvc.TagHelpers" 

The syntax for addTagHelper is tag name, assembly. In this example, * means a wildcard and adds all tags from the Microsoft.AspNet.Mvc.TagHelpers assembly.

Environment Tag Helper

One of the most useful tag helpers is the environment helper. This allows specific code to only run in specific environments. In this example, the Development environment will use the un-minified CSS and JavaScript, while the staging and production environments will use the minified and compacted files.

  <meta name="viewport" content="width=device-width" /> 
  <title>Untitled Page</title> 
  <environment names="Development"> 
    <link asp-href="~/css/*.css" rel="stylesheet" /> 
  <environment names="Staging,Production"> 
    <link href="/css/stylesheet.min.css" rel="stylesheet" /> 

You can also see the link tag helper in action as well. The asp-href tag will process all the CSS files in the CSS folder and output the tags for you.

Creating WebAPI is MVC6

When creating WebApi methods in the past, you had to create a controller that derives from the ApiController. Now that MVC and WebApi have been unified, this is no longer the case and API methods exist in plain old Controller classes. Although you can mix MVC and API methods in the same class, it is highly recommended that you separate out the two into separate namespaces and classes.

public class Customer : Controller
  private ICustomerService _customerService { get; set; }

  public Customer(ICustomerService customerService) 
    _customerService = customerService;

  public IActionResult List()
    return Ok(_customerService.List());

And that's a simple WebAPI controller. Note how it looks the same as a regular MVC controller, with the addition of the route attribute. This just tells the routing table that anything starting with api in the URL should go to the controller named, so in this examples /api/customer/list is the request URL.

2 thoughts on “What’s New in MVC6 and Why MVC6 is the Best Yet
  • 17th October 2018 at 12:00 am

    Great post. Fantastic. Thanks for sharing this information on MVC6 and WEB-API with clear explanation. I hope you will keep sharing more such informative articles.

  • 9th May 2016 at 12:00 am

    I think that what you said was very reasonable. However, what about this?
    what if you were to write a awesome post title?
    I ain't saying your content is not good., but
    what if you added something to possibly get folk's attention? I mean What's New in MVC6 and Why
    MVC6 is the Best Yet - ASP.Net - Tim Trott is kinda vanilla.
    You could glance at Yahoo's home page and watch how
    they write article headlines to grab people to click.
    You might add a video or a pic or two to get readers excited
    about what you've written. In my opinion, it would bring your posts a little bit more interesting.


Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.