Presentation for Dutch Microsoft TechDays 2015:
With ASP.NET 5 comes MVC 6 with a programming model that unifies Web Pages, MVC and Web API. Each of these has been rebuilt to reflect Microsoft's vision of lean and composable web applications. In this session you will see the changes that have been made to the programming model. We will cover topics such as the new POCO controllers, View Components, dependency injection and much more. Plus, you are going to see the significant changes to the ASP.NET runtime on which MVC 6 is built.
6. Characteristics of MVC 6
•One set of concepts – remove duplication
•Web UI and APIs combined
•Smooth transition from Web Pages to MVC
Unified
•Run on IIS or self-host or cross-platform
•Based on new request pipeline in ASP.NET 5
•Runs in cloud-optimized Core CLR
Built on top
of ASP.NET 5
•Pay as you use
•Middleware
•Built with Dependency Injection first
Modular
Getting familiar
7. .NET Framework vNext
Full .NET CLR
• Entire API set in machine
wide install at 200 MB
• Updated with OS
• Ecosystem of existing
packages
• Backward compatibility
• Default for
Visual Studio 2015 projects
Cloud Optimized CLR
• Lean and modular runtime
• Optimized for server
• Small memory footprint
• Libraries in NuGet packages
• Framework deployed with
app
• Different versions can run
side-by-side
• 11 MB
Mono
• Cross-Platform runtime for
Linux and Mac OS X
• Together with Mono
community
Standing on the shoulders of a new giant
ASP.NET 5.0 ASP.NET 5.0 ASP.NET 5.0
9. Middleware conceptually
• Middleware stack
• Static files
• Security, authentication,
CORS
• Diagnostics, logging
• Other cross-cutting concerns
Host process and server
Application and framework
RequestDelegate
delegate Task
RequestDelegate(HttpContext
context);
10. public void Configure(IApplicationBuilder app)
{
app.Map("/Nancy",
builder =>
{ builder.UseRuntimeInfo();
builder.RunNancyFx();
});
app.UseCors();
app.UseMvc(cfg);
Use, Map and Run
app.UseErrorPage();
Application FX
Middleware
11. Controllers
• Single base class for
MVC and Web API
• Not required
• POCO Controllers
• No base class
• Combine Web API and
MVC in 1 class
• UI and REST endpoints
at one base URI
Not your typical gaming gear
13. Dependency Injection
• DI is core part of runtime
• Built-in DI for configuration and services
– Default lightweight version available
• Wire up your own favorite IoC container ...
– Autofac, Ninject, StructureMap, Unity, Castle Windsor
public void ConfigureServices(IServiceCollection services)
{
// Add EF services to the services container.
services.AddEntityFramework(Configuration)
.AddSqlServer()
.AddDbContext<ApplicationDbContext>();
14. Built-in DI Container
• Register mappings in ConfigureServices of Startup
• Default registrations
• Lifetimes
– Transient
– Scoped
– Singleton
– Instance
• Rules for registering mapping
– First one wins
– Allows you to get ahead off default mappings
Sometimes better is worse than good enough
15. Activation
Activation method Example
Constructor on classes MyController(ILog logger)
Attribute on field or
property
[Activate]
ILog Logger { get;set; }
Attribute in action [FromServices] ILog logger
Directive in Razor page ILog logger
Inject those objects
@inject
16. Tag Helpers
• Allow server-side code to participate in rendering of
HTML elements in Razor files
• Target HTML elements based on element and attribute
names
• Reduce explicit transitions between HTML and C#
• Take advantage of composition and tooling benefits in
Visual Studio Code and 2015
Getting some help in Razor
17. Custom Tag Helpers
namespace TechDays.TagHelpers
{
[TargetElement("datumtijd")]
public class DateTimeTagHelper : TagHelper
{
public override void Process(TagHelperContext context,
TagHelperOutput output) {
output.Content.SetContent(DateTime.Now.ToString());
}
}
}
// Inside Razor .cshtml file
@addTagHelper "*, TechDays.TagHelpers"
18. Routing
• Familiar MVC 5 attribute based routing
• Enabled by default
• Special tokens: [controller] and [action]
Show me the way
[Route("api/Hello")]
public class HelloController : Controller
{
[Route]
public string Get()
{
return "hello";
}
}
[Route("api/[controller]")]
public class HelloController : Controller
{
[Route]
public string Get()
{
return "hello";
}
}
19. View components
• Comparable to Web Forms’ UserControls
• Different from Partial Views
• Combines code and view as logical unit
User controls in MVC style
20. Logging
• Pluggable logging infrastructure provided
• ILoggerFactory
• Logging levels
– from Debug up to Criticial
21. Summary
• Unified programming model
• Built on ASP.NET 5
• Learn Dependency Injection
• Command-line vs. Visual Studio
Key takeaways from ASP.NET MVC 6
22. Your feedback is important!
Scan the QR Code and let us know via the TechDays App.
Laat ons weten wat u van de sessie vindt!
Scan the QR Code via de TechDays App.
Bent u al lid van de Microsot Virtual Academy?! Op MVA kunt u altijd iets
nieuws leren over de laatste technologie van Microsoft. Meld u vandaag aan
op de MVA Stand. MVA biedt 7/24 gratis online training on-demand voor IT-
Professionals en Ontwikkelaars.