static void

ASP.Net Core Views

Views aren't much different to MVC5 (notes)

You can't precompile Razor views in Core v1 (<MvcBuildViews>false</MvcBuildViews> in csproj) - cshtml files must be deployed unless you embed them and use EmbeddedFilesProvider

Nuget packages

You can use Razor views without any packages.

View Location

Instead of the default folders (controllers, models, views) some people prefer "feature folders", where the project has a folder per feature; controllers live alongside the model and view.

You need to tell MVC where to find the views...

Injecting services

You should be populating your view model (or ViewBag) with all the data for the page. We can now inject services into the view (which uses dependency injection). This sounds like the wrong thing to do, but at least one scenario (localization) it makes sense.


Instead of @Html.ActionLink("Log in", "Account", "Login"), use a taghelper (and it's easier to add a class or other attributes)

<a asp-controller="Account" asp-action="Login">Log in</a>

The code-like @using (Html.BeginForm()) becomes much nicer

<form asp-controller="Product" asp-action="Update" method="post" class="bootstrappy-stuff" role="form">
        <input asp-for="@product.ProductName" type="text" />
        <input asp-for="@product.ProductId" type="text" />
        <input type="submit" class="btn btn-default" value="Update" title="Update @account.ProductName to new value" />

In _layout.cshtml

<environment names="Development">
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>

To run the taghelpers, you need a Views/_ViewImports.cshtml. This is a magic file (like _ViewStart.cshtml which indicates the layout page, Shared/_Layout.cshtml).

@using MyProject
@using MyProject.Models
@using Microsoft.AspNet.Identity
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

Add an additional @addTagHelper  line for other assemblies containing taghelpers.

You can "escape" taghelpers by adding a ! prefix to the html-element- <!a asp-controller="Account" /> would send write an <a> element with the asp-controller attribute to the browser.