SlideShare a Scribd company logo
.NET Core + ASP.NET Core Training Course
Session 18
.NET Core
What we learned?
Session 6 ~ 16 Overview
• ASP.NET Core Basics
• Middleware, Controllers, Action Filters and Routing
• Models
• Views
• Entity Framework Core
.NET Core
What we’ll learn today?
Session 16 Agenda
• Welcome to ASP.NET Core 1.1
.NET Core
Changelog
ASP.NET Core 1.1
Performance
• ASP.NET Core running on Linux is approximately 760 times faster than it was one
year ago (TechEmpower benchmarks)
.NET Core
Changelog
ASP.NET Core 1.1
What’s New
• Improved and cross-platform compatible site hosting capabilities when using a
host other than Windows Internet Information Server (IIS).
• Support for developing with native Windows capabilities
• Compatibility, portability and performance of middleware and other MVC
features throughout the UI framework
• Improved deployment and management experience of ASP.NET Core
applications on Microsoft Azure. We think these improvements help make
ASP.NET Core the best choice for developing an application for the cloud.
.NET Core
Changelog
ASP.NET Core 1.1
What’s New
• Improved and cross-platform compatible site hosting capabilities when using a
host other than Windows Internet Information Server (IIS).
• Support for developing with native Windows capabilities
• Compatibility, portability and performance of middleware and other MVC
features throughout the UI framework
• Improved deployment and management experience of ASP.NET Core
applications on Microsoft Azure. We think these improvements help make
ASP.NET Core the best choice for developing an application for the cloud.
.NET Core
URL Rewriting Middleware
ASP.NET Core 1.1
URL rewriting functionality to ASP.NET Core through a middleware component that
can be configured using IIS standard XML formatted rules, Apache Mod_Rewrite
syntax, or some simple C# methods coded into your application.
When you want to run your ASP.NET Core application outside of IIS, we want to
enable those same rich URL rewriting capabilities regardless of the web host you
are using. If you are using containers, Apache, or nginx you will be able to have
ASP.NET Core manage this capability for you with a uniform syntax that you are
familiar with.
.NET Core
URL Rewriting Middleware
ASP.NET Core 1.1
URL Rewriting allows mapping a public URL space, designed for consumption of
your clients, to whatever representation the downstream components of your
middleware pipeline require as well as redirecting clients to different URLs based
on a pattern.
For example, you could ensure a canonical hostname by rewriting any requests to
http://example.com to instead be http://www.example.com for everything after the re-write rules
have run. Another example is to redirect all requests to http://example.com to https://example.com.
You can even configure URL rewrite such that both rules are applied and all requests to
example.com are always redirected to SSL and rewritten to www.
.NET Core
URL Rewriting Middleware
ASP.NET Core 1.1
• Url Redirect sends an HTTP 301 Moved Permanently status code to the client with the new address
• Url Rewrite gives a different URL to the next steps in the HTTP pipeline, tricking it into thinking a different
address was requested.
.NET Core
Response Caching Middleware
ASP.NET Core 1.1
Response Caching similar to the OutputCache capabilities of previous ASP.NET releases can
now be activated in your application by adding the Microsoft.AspNetCore.ResponseCaching
and the Microsoft.Extensions.Caching.Memory packages to your application. You can add
this middleware to your application in the Startup.ConfigureServices method and configure
the response caching from the Startup.Configure method.
You can now add GZipCompression to the ASP.NET HTTP Pipeline if you would like ASP.NET
to do your compression instead of a front-end web server. IIS would have normally handled
this for you, but in environments where your host does not provide compression capabilities,
ASP.NET Core can do this for you.
.NET Core
Response Caching Middleware
ASP.NET Core 1.1
Microsoft.AspNetCore.ResponseCompression package
.NET Core
WebListener Server for Windows
ASP.NET Core 1.1
WebListener is a server that runs directly on top of the Windows Http Server API.
WebListener gives you the option to take advantage of Windows specific features, like
• support for Windows authentication
• port sharing
• HTTPS with SNI
• HTTP/2 over TLS (Windows 10)
• direct file transmission
• response caching WebSockets (Windows 8)
This may be advantageous for you
if you want to bundle an ASP.NET
Core microservice in a Windows
container that takes advantage of
these Windows features.
.NET Core
WebListener Server for Windows
ASP.NET Core 1.1
On Windows you can use this server instead of Kestrel by referencing the
Microsoft.AspNetCore.Server.WebListener package instead of the Kestrel package and
configuring your WebHostBuilder to use Weblistener instead of Kestrel:
.NET Core
View Components as Tag Helpers
ASP.NET Core 1.1
ViewComponents are an ASP.NET Core display concept that provides for a razor view
that is triggered from a server-side class that inherits from the ViewComponent base
class. You can now invoke from your views using Tag Helper syntax and get all the
benefits of IntelliSense and Tag Helper tooling in Visual Studio. Previously, to invoke a
View Component from a view you would use the Component.InvokeAsync method and
pass in any View Component arguments using an anonymous object:
@await Component.InvokeAsync("Copyright", new { website = "example.com", year = 2016 })
.NET Core
View Components as Tag Helpers
ASP.NET Core 1.1
With the Component.Invoke syntax, there is no obvious way to add CSS classes or get
tooltips to assist in configuring the component like we have with the TagHelper feature.
Finally, this keeps us in “HTML Editing” mode and allows a developer to avoid shifting
into C# in order to reference a ViewComponent they want to add to a page.
To enable invoking your View Components as Tag Helpers simply add your View
Components as Tag Helpers using the @addTagHelpers directive:
@addTagHelper "*, WebApplication1"
.NET Core
Middleware as MVC filters
ASP.NET Core 1.1
Middleware typically sits in the global request handling pipeline.
You can now apply middleware as an MVC resource filter using the new
MiddlewareFilterAttribute. For example, you could apply response compression or
caching to a specific action, or you might use a route value based request culture
provider to establish the current culture for the request using the localization
middleware.
.NET Core
Middleware as MVC filters
ASP.NET Core 1.1
.NET Core
Middleware as MVC filters
ASP.NET Core 1.1
.NET Core
Cookie-based TempData provider
ASP.NET Core 1.1
To use the cookie-based TempData provider you register the
CookieTempDataProvider service in your ConfigureServices method after adding the
MVC services as follows:
.NET Core
View compilation
ASP.NET Core 1.1
The Razor syntax for views provides a flexible development experience where
compilation of the views happens automatically at runtime when the view is executed.
However, there are some scenarios where you do not want the Razor syntax compiled
at runtime. You can now compile the Razor views that your application references and
deploy them with your application.
.NET Core
View compilation
ASP.NET Core 1.1
To enable view compilation as part of publishing your application,
1. Add a reference to “Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design”
under the “dependencies” section.
2. Add a reference to “Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools”
under the tools section
3. Add a postpublish script to invoke view compiler:
.NET Core
Azure App Service logging provider
ASP.NET Core 1.1
The Microsoft.AspNetCore.AzureAppServicesIntegration package allows your
application to take advantage of App Service specific logging and diagnostics. Any
log messages that are written using the ILogger/ILoggerFactory abstractions will go
to the locations configured in the Diagnostics Logs section of your App Service
configuration in the portal (see screenshot). We highly recommend using this
logging provider when deploying an application to Azure App Service. Prior to this
feature, it was very difficult to capture log files without a third party provider or
hosted service.
.NET Core
Azure Key Vault configuration provider
ASP.NET Core 1.1
Azure Key Vault is a service that can be used to store secret cryptographic keys and
other secrets in a security hardened container on Azure. You can set up your own Key
Vault by following the Getting Started docs. The
Microsoft.Extensions.Configuration.AzureKeyVault package then provides a
configuration provider for your Azure Key Vault. This package allows you to retrieve
configuration from Key Vault secrets on application start and hold it in memory, using
the normal ASP.NET Core configuration abstractions to access the configuration data.
.NET Core
Redis and Azure Storage Data Protection Key Repositories
ASP.NET Core 1.1
The Microsoft.AspNetCore.DataProtection.AzureStorage and
Microsoft.AspNetCore.DataProtection.Redis packages allow storing your Data
Protection keys in Azure Storage or Redis respectively. This allows keys to be shared
across several instances of a web application so that you can share an authentication
cookie, or CSRF protection across many load balanced servers running your ASP.NET
Core application. As data protection is used behind the scenes for a few things in
MVC it’s extremely probable once you start scaling out you will need to share the
keyring. Your options for sharing keys before these two packages would be to use a
network share with a file based key repository.
.NET Core
Redis and Azure Storage Data Protection Key Repositories
ASP.NET Core 1.1
The Microsoft.AspNetCore.DataProtection.AzureStorage and
Microsoft.AspNetCore.DataProtection.Redis packages allow storing your Data
Protection keys in Azure Storage or Redis respectively. This allows keys to be shared
across several instances of a web application so that you can share an authentication
cookie, or CSRF protection across many load balanced servers running your ASP.NET
Core application. As data protection is used behind the scenes for a few things in
MVC it’s extremely probable once you start scaling out you will need to share the
keyring. Your options for sharing keys before these two packages would be to use a
network share with a file based key repository.
.NET Core
Demo
Demo

More Related Content

.NET Core, ASP.NET Core Course, Session 18

  • 1. .NET Core + ASP.NET Core Training Course Session 18
  • 2. .NET Core What we learned? Session 6 ~ 16 Overview • ASP.NET Core Basics • Middleware, Controllers, Action Filters and Routing • Models • Views • Entity Framework Core
  • 3. .NET Core What we’ll learn today? Session 16 Agenda • Welcome to ASP.NET Core 1.1
  • 4. .NET Core Changelog ASP.NET Core 1.1 Performance • ASP.NET Core running on Linux is approximately 760 times faster than it was one year ago (TechEmpower benchmarks)
  • 5. .NET Core Changelog ASP.NET Core 1.1 What’s New • Improved and cross-platform compatible site hosting capabilities when using a host other than Windows Internet Information Server (IIS). • Support for developing with native Windows capabilities • Compatibility, portability and performance of middleware and other MVC features throughout the UI framework • Improved deployment and management experience of ASP.NET Core applications on Microsoft Azure. We think these improvements help make ASP.NET Core the best choice for developing an application for the cloud.
  • 6. .NET Core Changelog ASP.NET Core 1.1 What’s New • Improved and cross-platform compatible site hosting capabilities when using a host other than Windows Internet Information Server (IIS). • Support for developing with native Windows capabilities • Compatibility, portability and performance of middleware and other MVC features throughout the UI framework • Improved deployment and management experience of ASP.NET Core applications on Microsoft Azure. We think these improvements help make ASP.NET Core the best choice for developing an application for the cloud.
  • 7. .NET Core URL Rewriting Middleware ASP.NET Core 1.1 URL rewriting functionality to ASP.NET Core through a middleware component that can be configured using IIS standard XML formatted rules, Apache Mod_Rewrite syntax, or some simple C# methods coded into your application. When you want to run your ASP.NET Core application outside of IIS, we want to enable those same rich URL rewriting capabilities regardless of the web host you are using. If you are using containers, Apache, or nginx you will be able to have ASP.NET Core manage this capability for you with a uniform syntax that you are familiar with.
  • 8. .NET Core URL Rewriting Middleware ASP.NET Core 1.1 URL Rewriting allows mapping a public URL space, designed for consumption of your clients, to whatever representation the downstream components of your middleware pipeline require as well as redirecting clients to different URLs based on a pattern. For example, you could ensure a canonical hostname by rewriting any requests to http://example.com to instead be http://www.example.com for everything after the re-write rules have run. Another example is to redirect all requests to http://example.com to https://example.com. You can even configure URL rewrite such that both rules are applied and all requests to example.com are always redirected to SSL and rewritten to www.
  • 9. .NET Core URL Rewriting Middleware ASP.NET Core 1.1 • Url Redirect sends an HTTP 301 Moved Permanently status code to the client with the new address • Url Rewrite gives a different URL to the next steps in the HTTP pipeline, tricking it into thinking a different address was requested.
  • 10. .NET Core Response Caching Middleware ASP.NET Core 1.1 Response Caching similar to the OutputCache capabilities of previous ASP.NET releases can now be activated in your application by adding the Microsoft.AspNetCore.ResponseCaching and the Microsoft.Extensions.Caching.Memory packages to your application. You can add this middleware to your application in the Startup.ConfigureServices method and configure the response caching from the Startup.Configure method. You can now add GZipCompression to the ASP.NET HTTP Pipeline if you would like ASP.NET to do your compression instead of a front-end web server. IIS would have normally handled this for you, but in environments where your host does not provide compression capabilities, ASP.NET Core can do this for you.
  • 11. .NET Core Response Caching Middleware ASP.NET Core 1.1 Microsoft.AspNetCore.ResponseCompression package
  • 12. .NET Core WebListener Server for Windows ASP.NET Core 1.1 WebListener is a server that runs directly on top of the Windows Http Server API. WebListener gives you the option to take advantage of Windows specific features, like • support for Windows authentication • port sharing • HTTPS with SNI • HTTP/2 over TLS (Windows 10) • direct file transmission • response caching WebSockets (Windows 8) This may be advantageous for you if you want to bundle an ASP.NET Core microservice in a Windows container that takes advantage of these Windows features.
  • 13. .NET Core WebListener Server for Windows ASP.NET Core 1.1 On Windows you can use this server instead of Kestrel by referencing the Microsoft.AspNetCore.Server.WebListener package instead of the Kestrel package and configuring your WebHostBuilder to use Weblistener instead of Kestrel:
  • 14. .NET Core View Components as Tag Helpers ASP.NET Core 1.1 ViewComponents are an ASP.NET Core display concept that provides for a razor view that is triggered from a server-side class that inherits from the ViewComponent base class. You can now invoke from your views using Tag Helper syntax and get all the benefits of IntelliSense and Tag Helper tooling in Visual Studio. Previously, to invoke a View Component from a view you would use the Component.InvokeAsync method and pass in any View Component arguments using an anonymous object: @await Component.InvokeAsync("Copyright", new { website = "example.com", year = 2016 })
  • 15. .NET Core View Components as Tag Helpers ASP.NET Core 1.1 With the Component.Invoke syntax, there is no obvious way to add CSS classes or get tooltips to assist in configuring the component like we have with the TagHelper feature. Finally, this keeps us in “HTML Editing” mode and allows a developer to avoid shifting into C# in order to reference a ViewComponent they want to add to a page. To enable invoking your View Components as Tag Helpers simply add your View Components as Tag Helpers using the @addTagHelpers directive: @addTagHelper "*, WebApplication1"
  • 16. .NET Core Middleware as MVC filters ASP.NET Core 1.1 Middleware typically sits in the global request handling pipeline. You can now apply middleware as an MVC resource filter using the new MiddlewareFilterAttribute. For example, you could apply response compression or caching to a specific action, or you might use a route value based request culture provider to establish the current culture for the request using the localization middleware.
  • 17. .NET Core Middleware as MVC filters ASP.NET Core 1.1
  • 18. .NET Core Middleware as MVC filters ASP.NET Core 1.1
  • 19. .NET Core Cookie-based TempData provider ASP.NET Core 1.1 To use the cookie-based TempData provider you register the CookieTempDataProvider service in your ConfigureServices method after adding the MVC services as follows:
  • 20. .NET Core View compilation ASP.NET Core 1.1 The Razor syntax for views provides a flexible development experience where compilation of the views happens automatically at runtime when the view is executed. However, there are some scenarios where you do not want the Razor syntax compiled at runtime. You can now compile the Razor views that your application references and deploy them with your application.
  • 21. .NET Core View compilation ASP.NET Core 1.1 To enable view compilation as part of publishing your application, 1. Add a reference to “Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design” under the “dependencies” section. 2. Add a reference to “Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools” under the tools section 3. Add a postpublish script to invoke view compiler:
  • 22. .NET Core Azure App Service logging provider ASP.NET Core 1.1 The Microsoft.AspNetCore.AzureAppServicesIntegration package allows your application to take advantage of App Service specific logging and diagnostics. Any log messages that are written using the ILogger/ILoggerFactory abstractions will go to the locations configured in the Diagnostics Logs section of your App Service configuration in the portal (see screenshot). We highly recommend using this logging provider when deploying an application to Azure App Service. Prior to this feature, it was very difficult to capture log files without a third party provider or hosted service.
  • 23. .NET Core Azure Key Vault configuration provider ASP.NET Core 1.1 Azure Key Vault is a service that can be used to store secret cryptographic keys and other secrets in a security hardened container on Azure. You can set up your own Key Vault by following the Getting Started docs. The Microsoft.Extensions.Configuration.AzureKeyVault package then provides a configuration provider for your Azure Key Vault. This package allows you to retrieve configuration from Key Vault secrets on application start and hold it in memory, using the normal ASP.NET Core configuration abstractions to access the configuration data.
  • 24. .NET Core Redis and Azure Storage Data Protection Key Repositories ASP.NET Core 1.1 The Microsoft.AspNetCore.DataProtection.AzureStorage and Microsoft.AspNetCore.DataProtection.Redis packages allow storing your Data Protection keys in Azure Storage or Redis respectively. This allows keys to be shared across several instances of a web application so that you can share an authentication cookie, or CSRF protection across many load balanced servers running your ASP.NET Core application. As data protection is used behind the scenes for a few things in MVC it’s extremely probable once you start scaling out you will need to share the keyring. Your options for sharing keys before these two packages would be to use a network share with a file based key repository.
  • 25. .NET Core Redis and Azure Storage Data Protection Key Repositories ASP.NET Core 1.1 The Microsoft.AspNetCore.DataProtection.AzureStorage and Microsoft.AspNetCore.DataProtection.Redis packages allow storing your Data Protection keys in Azure Storage or Redis respectively. This allows keys to be shared across several instances of a web application so that you can share an authentication cookie, or CSRF protection across many load balanced servers running your ASP.NET Core application. As data protection is used behind the scenes for a few things in MVC it’s extremely probable once you start scaling out you will need to share the keyring. Your options for sharing keys before these two packages would be to use a network share with a file based key repository.