It is almost there, the RTM version of ASP.NET 5 and MVC6, with a new project system in Visual Studio 2015 and a new cross-platform runtime! In this session we will dig into each one of those. We will look at how you can deploy your website without it being affected, or affecting other runtimes. We will look at how to build your own middleware for this new runtime (and maybe package it as a NuGet package), building POCO controllers for MVC6, how to do unit testing, … At the end of this session you should have a better insight in this exciting new framework and runtime!
7. Your own middleware
• Build class implementing
• Use extension method to install it
Task Invoke(HttpContext context)
public static IApplicationBuilder UsePrimeChecker(
this IApplicationBuilder builder, PrimeCheckerOptions options)
=> builder.Use(next => new Middleware(next, options).Invoke);
}
9. Unit Testing
• Fully supported in .NET Core 1.0
• Need to add the test runner as a command
"dependencies": {
"xunit": "2.1.0",
"xunit.runner.dnx": "2.1.0-rc1-build204",
"Microsoft.AspNet.TestHost": "1.0.0-rc1-final",
"IsPrime": "1.0.0-*"
},
"commands": {
"test": "xunit.runner.dnx"
}
10. Integration Testing
• ASP.NET Core 1.0 supports testing website without host
// Arrange
var server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>());
var client = server.CreateClient();
// Act
string request = "/checkprime?5";
var response = await client.GetAsync(request);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal("5 is prime!", responseString);
12. Taghelpers
• Enable us to write nicer razor code
• Look a lot like angular.js and knockout.js
• Example:
<ul class="nav navbar-nav">
<li><a asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-controller="Home" asp-action="About">About</a></li>
<li><a asp-controller="Home" asp-action="Contact">Contact</a></li>
</ul>
13. HtmlHelper vs. Tag helper
• Is there any doubt?
@Html.EditorFor(model => model.Title,
htmlAttributes: new { @class = "form-control"})
<input asp-for="Title" type="text" class="form-control" />
14. Building a custom tag helper
<ul u2u-property="Address.City" u2u-items="Model"></ul>
16. Using C#6 nameof instead of strings
• Again?!
• I want this!
• But it is not supported…
• I want this!
• So let’s add it…
<a asp-controller="@nameof(HomeController)"
asp-action="@nameof(HomeController.Index)">Home</a>
<a asp-controller="Home" asp-action="Index">Home</a>
17. Use the Source Luke!
• Debugging with MVC Sources
• Simply add path to sources in global.json
{
"projects": [ "src", "test", "C://Mvc//src" ],
"sdk": {
"version": "1.0.0-rc1-final"
}
}