Website development and design blog, tutorials and inspiration

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

We take a look at the changes in MVC6 and what they mean for developers

By , in C#

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. 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
ControllerApiController
ActionsActions
FiltersFilters
Model BindingModel Binding
RoutingRouting
AttributesAttributes

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.

  1. <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.

  1. @{
  2.  
  3. @addTagHelper "*,Microsoft.AspNet.Mvc.TagHelpers"
  4.  
  5. }

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.

  1. <head>
  2. <meta name="viewport" content="width=device-width" />
  3. <title>Untitled Page</title>
  4. <environment names="Development">
  5. <link asp-href="~/css/*.css" rel="stylesheet" />
  6. </environment>
  7. <environment names="Staging,Production">
  8. <link href="/css/stylesheet.min.css" rel="stylesheet" />
  9. </environment>

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.

  1. [Route("api/[controller]")]
  2. public class Customer : Controller
  3. {
  4. private ICustomerService _customerService { get; set; }
  5.  
  6. public Customer(ICustomerService customerService)
  7. {
  8. _customerService = customerService;
  9. }
  10.  
  11. [HttpGet("list")]
  12. public IActionResult List()
  13. {
  14. return Ok(_customerService.List());
  15. }
  16. }

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.

Last updated: 2017-09-08

Further Reading
Comments
  1. tipos de aborto
    tipos de aborto

    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

Your email address will not be published.