Tim TrottTim TrottDo or do not, there is no try

What’s New in MVC6 and Why MVC6 is the Best Yet

By , Tuesday 8th March 2016 in ASP.Net

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 separate 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 though 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 fourth between developer and designer and the tags do not break the designers 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.

My website and its content are free to use without the clutter of adverts, tracking cookies, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

About the Author

Tim Trott

Tim is a professional software engineer, designer, photographer and astronomer from the United Kingdom. You can follow him on Twitter to get the latest updates.

Further Reading
Leave a Reply

Your email address will not be published.