ASP.NET Core Deep Dive into MVC at Build 2016

ASP.NET Core is a new open-source and cross-platform framework for building modern applications. Previously called ASP.NET 5. Renamed and V1 version. Everything is modular, you can pull in from Nuget. Much faster development cycle because Nuget. ASP.NET Core designed and optimized for the cloud. ASP.NET Core is all about choice.

You can get started with the latest bit available: ASP.NET 5 RC 1 at get.asp.net. For compatibility you may still want to use .Net Framework.

Asp1

With ASP.NET Core you get Kestrel and Startup hosting, Middleware, built-in dependency injection, a new light-weight configuration system (there is no web.config), logging and application frameworks like MVC and coming soon SignalR.

Asp2

ASP.NET Core should be a unified web stack. ASP.NET Core MVC. Web UI and Wb APIs. Only one set of concepts. Supports .Net Core. New Tag Helpers. Built on ASP.NET Core. You can run in IIS or self-hosted.

New ASP.NET project templates dialog lists 4.6.1 and 5 templates.We have to restore the packages when we create a project. The controllers and views look the same. What makes it a .Net Core project is the project.json file. You can specify to cross compile for multiple frameworks in project.json.

Startup is what defines your ASP.NET Core application.

  • You have a Main() method. ASP.NET Core apps are just like console apps, which is why we have Main() method. You can add configuration with the ConfigurationBuilder. You can add user secrets, configuration source for your settings, and use them at runtime, to move them out of the project in order not to get them accidentally exposed to the public.
  • Configure() is the meat of your app, where you set up the requests. MVC is just routing middleware. Routing with inline constraints like routes.MapRoute(name: “default”, template: “{controller=Home}/{action=Index}/{id:int?}”).
  • ConfigureServices() is where you configure the services injectible to your application, you can also inject services into your views with @inject.

Razor pages are now also rendered asynchronously with @await Html.PartialAsync. New is also the ViewImports to set up your @using namespace directive and tag helpers with @addTagHelper. With tag helpers the page looks like HTML, it’s purple because Visual Studio wants to show that it’s a special HTML. Many tag helpers provided like validation summary, anchor, label, input and also new tag helpers like environment for what environment you are currently running in (Development, Staging or Production). @await FlushAsync() for flushing a view to load a page faster with the server sending the initial part of the HTML document before the complete response is ready. Server-side caching feature with the <cache>. Writing custom tag helper is very easy. This was Web UI.

Let’s do some Web API. We start with a blank ASP.NET 5 project template, add Microsoft.AspNet.Mvc.Core and add a controller with a method to get a value. Add routing information with a new feature called route tokens for defining route without having to repeat it. That’s it. No view, no Razor. Use Swagger to get nice visualization of the endpoints.

Advertisements