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

There are lots of changes introduced in MVC6, not least of which is a new view engine to replace Razor. Here we look at changes new to MVC6.

By Tim Trott | C# ASP.Net MVC | March 8, 2016
839 words, estimated reading time 3 minutes.

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 is a lot of duplication between them.

MVC Web Api
Controller ApiController
Actions Actions
Filters Filters
Model Binding Model Binding
Routing Routing
Attributes Attributes

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 nor WebAPI use System.Web, the 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 one below to render a text box.

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

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

xml
<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 Microsoft.AspNet.Mvc.TagHelpers assembly. You can add this to each page as well, or specific tags on certain pages.

xml
@{ 
 
@addTagHelper "*,Microsoft.AspNet.Mvc.TagHelpers" 
 
}

The syntax for addTagHelper is the 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.

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

You can 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 the two into separate namespaces and classes.

C#
[Route("api/[controller]")]
public class Customer : Controller
{
  private ICustomerService _customerService { get; set; }

  public Customer(ICustomerService customerService) 
  {
    _customerService = customerService;
  }

  [HttpGet("list")]
  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 example /api/customer/list is the request URL.

Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

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.

This post has 1 comment(s). Why not join the discussion!

We respect your privacy, and will not make your email public. Learn how your comment data is processed.

  1. JS

    On Wednesday 17th of October 2018, JATINDAR SINGH said

    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.