mvc 6 - the new unified web programming model
TRANSCRIPT
Alex ThissenLead Consultant Xpirit
ASP.NET MVC 6A new unified programming model for the web
Agenda• Short overview ASP.NET 5• A unified programming model• Controllers• Dependency Injection• Routing
• Summary• Questions and Answers
An overviewViewing from 10.000 feet
ASP.NET components
MVC 6Unified Web Stack
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
.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
Getting started
• Middleware model introduced by OWIN
Ready, set, start – a lap around MVC 6
Middleware conceptually
• Middleware stack• Static files• Security, authentication,
CORS• Diagnostics, logging• Other cross-cutting
concernsHost process and server
Application and framework
Host
ApplicationApplication Framework
Server
RequestDelegatedelegate Task RequestDelegate(HttpContext context);
public void Configure(IApplicationBuilder app) {
app.Map("/Nancy", builder => { builder.UseRuntimeInfo();
builder.RunNancyFx(); });
app.UseCors();
app.UseMvc(cfg);
Cors
Use, Map and Run
ErrorPageapp.UseErrorPage();
RuntimeInfo
NancyFX
MVC+WebAPI
Application FX
Middleware
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
Dependency InjectionIt all depends
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 Windsorpublic void ConfigureServices(IServiceCollection services)
{ // Add EF services to the services container. services.AddEntityFramework(Configuration) .AddSqlServer() .AddDbContext<ApplicationDbContext>();
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
ActivationActivation 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
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
Custom Tag Helpersnamespace 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"
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"; }}
View components
• Comparable to Web Forms’ UserControls• Different from Partial Views• Combines code and view as logical unit
User controls in MVC style
Logging
• Pluggable logging infrastructure provided• ILoggerFactory• Logging levels
– from Debug up to Criticial
Summary• Unified programming model• Built on ASP.NET 5• Learn Dependency Injection• Command-line vs. Visual Studio
Key takeaways from ASP.NET MVC 6
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.