ASP.NET Core MVC & Razor Pages in .NET 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
>> We got our friend Dan Roth again. Hey, I recognize you. >> Hello, I'm back. >> Welcome back. >> I think we have a two for one here actually. >> We do. We've got you twice. You're going to talk about ASP.NET Core, MVC, and Razor Pages in.NET 6. Take it away, sir. >> Right. Hello everyone again, I am Dan Roth. I'm a Program Manager on the ASP.NET team. We're going to take a look at the new features in ASP.NET Core in.NET 6 specifically for Razor Pages and MVC. But we'll also take a broad look at all the new things in ASP.NET Core, broadly speaking. ASP.NET Core is a modern web framework for.NET. It has everything that you need to build rich modern web UI, as well as powerful back-end services. Regardless of what type of app you're trying to build, whether it's a web app, server or Cloud, ASP.NET Core offers a complete, and integrated solution for building your web applications. Now there's a lot of new stuff in ASP.NET Core in.NET 6 and we're just going to cover some of the highlights here briefly. First of all, there's the new simplify getting started experience with minimal APIs. If you missed Stephan and Sophia's talk earlier, make sure you go and check it out where they talked through all of the details of this new model. But we'll look at it a little bit ourselves as well. In.NET 6, you can get productive with your ASP.NET Core apps using.NET Hot Reload, all of the ASP.NET Core product types support hot reload. It has a cross-cutting.NET 6 feature. Faster performance and lower memory usage. Performance is part of our ASP.NET Core team's culture. Every release we try and squeeze out even more performance and this release, I hope, did not disappoint. We have better integration with Front End JavaScript Frameworks in.NET 6 for folks that are building JavaScript with an ASP.NET Core backend. We also have improved support for client web UI with.NET and Blazor. Now getting started with ASP.NET Core and.NET 6 is easier than ever before. We've dramatically simplified what it takes to build your first ASP.NET Core app. We've simplified the ASP.NET Core hosting model and then introduced a new minimal way to build APIs that we call minimal APIs with middle effort, but without compromising on features and productivity. We've also updated all of the templates to use the latest C# features, top-level statements, global usings, file scope namespaces. We turned on null check and Allstate checking in the template says as well. Definitely go in and look at Sophia and Stephan talk about this topic about Min APIs. We'll only touch on them briefly here. Performance.NET 6 Is not just about simplicity, it's also about performance. The more performance and the more efficient your server resource utilization. The http requests handling pipeline in.NET 6 is 5 percent faster than it was in. NET 5 and that's just across the board, the core of the pipeline. For a minimal APIs. Minimal APIs basically give you the performance of HTTP middleware, ASP.NET Core middleware, but with the flexibility and productivity of API controllers. The equivalent main API for an API controller is about twice as fast in terms of throughput. Authentication in.NET 6 is much faster In ASP.NET Core. JWT authentication is 40 percent faster than in earlier releases and this is thanks to some really great work by the Identity team at Microsoft in their JWT parsing library. This doesn't just benefit ASP.NET Core, it benefits anyone using the common libraries functionality. MVC on Linux. We care about making sure ASP.NET Core runs great everywhere. On Linux, MVC is now 12 percent faster thanks to some optimization that we made around the logging pipeline to make that more efficient. We also have more efficient memory cache in.NET 6 with a 10 percent improvement in requests per second. That's a gain on the cash TechEmpower benchmark. Much of the performance work that we do in ASP.NET Core is based on the TechEmpower benchmarks as a baseline. Memory usage is reduced in. NET 6. A working set for HTTPS connections is 70 percent less for idle WebSocket connections in.NET 6, a huge win. For HTTP2 and gRPC, bunch of performance improvements there is as well. We've made improvements to the HP client and its connection management and Protobuf serialization is 20 percent faster on.NET 6, so great performance improvements to look forward to. We also want web development to be fast, fluid, iterative, and productive. We've made optimizations to build time and startup performance in ASP.NET Core across the board. One of those optimizations we made was redoing the Razor compiler. I think that takes your Razor files and turns it into runnable code. We changed it from using its two-phase approach to instead use C# source generators. In addition to proving perf, this also enabled support for editing continue in Razor files, as well as support for.NET Hot Reload,.NET Hot Reload is the ability to make changes to your code and apply them to the running app without having to rebuild it or restart it. It just gets patched into their running application. All of the ASP.NET Core product types support.NET Hot Reload in.NET 6. All right, for MVC and Razor Pages. Here's the list of things that are new. We now have support for CSS isolation for your pages and views. This is a feature that we originally added to blazer for blazer components, where you could define a CSS style sheet that was specific to a single component and it would be isolated to that component. We've brought that feature over for NBC in Razor pages as well. You can now have a CSHTML.CSS file that's a companion to your pages and views. We've added support for Async streaming in MVC so that you can return an AsyncEnumerable from your controllers and have that be asynchronously streamed down to the client without all of the entries having to be buffered in memory on the server. This was a collaboration done with the.NET Framework teams who work on the JSON serializer plum that all the way down through the stack. All the ASP.NET Core templates have been updated to use the latest version of Bootstrap, Bootstrap 5.1. We've also done some simple improvements to the open API support in ASP.NET Core and we added support for optional parameters for view component tag helpers. I think this is a great example of engagement with the GitHub community. This was one of the top voted items in the MVC backlog, and so we got it done in.NET 6. Thank you for all the feedback that you provide. You help make this release successful. All right, so let's go ahead and take a look at some of these new features. Let's hop over here to Visual Studio. What we're going to do is we're going to create a new project and this time we're going to create an ASP.NET Core web application. This one will be based on Razor Pages. All right, that looks like a fine name for this web app and of course we're going to use.NET 6, our new LTS release. Let's go ahead and create that. When you look at this project, I hope the first thing you notice is, wow, it does look simpler, like there's just less stuff here. This product has been updated to use the new minimal hosting pattern in ASP.NET Core. It doesn't have a startup class. You can still use startup classes in.NET 6, they're still absolutely supported. We have a very high compat bar for each of our.NET releases. But for new projects, this is a really nice and simple way to get started. If we look at program CS, all of the logic that we would normally have in our startup class now is surfaced here. Above, we have the place where we can configure our services for dependency injection. Here we're configuring the services for Razor Pages. Then down below we configure our middleware request handling pipeline. This file is using the latest greatest C# features, so we've got a top-level statements. There's no need for a program main. There's no using directives at the top because we're using implicit usings. Then all the other files in the template are also taking advantage of file scope namespaces and nullability checking is also enabled. Let's go ahead and run this and see what the app looks like. Here is our.NET 6 ASP.NET Core app and it looks very similar to previous releases, but if we go and check in the DevTools just like we did with the blazer apps earlier today. Let's go look in the sources and I think this one's tucked away under lib bootstrap dist CSS, right there, bootstrap.min.css. We are in fact using the latest version of Bootstrap in all the ASP.NET Core templates, so you get all the benefits of that version of Bootstrap. Let's make some code changes to this application. I'm going to pin this over to the side and bring over Visual Studio. Let's give ourselves a little bit more coding room. Let's go. Where do want to start? Start in Index.cshtml, which is the homepage. Let's just make some code changes. Welcome to.net. Let me just make sure I can type.Netconf 2021! And I'm going to hit the Flaming Hot Reload button. We'll apply that to our running application and it just shows up. Hot Reload is supported in CSHTML just like it is in Razor files and C-Sharp files across the.net application. Let's do some more changes in here, and let's do the first razor page that anyone writes, which is the current time. The current time is DateTime.now and we can now, I've already enabled that Hot Reload on Save feature, so we're going to go ahead and just save this file and that immediately then shows up in the browser, right there. Looks great. We can use tag helpers too. Let's add a tag helper into this file environment and we'll grab the code that we had before and we'll stick that inside this environment tag helper. If you're not familiar with this tag helper, what it does is it will only render its content if you're running in the specified environment, the specified ASP.NET Core environment like development versus production. Let's include, in this case let's render only in production. I'm not in production right now. If I Hot Reload this, this should make it disappear and it does and then if I go back into development and we Hot Reload that change, then we should see our current time again. This works with TagHelpers. We can also, of course, chains are C-Sharp code. Up here at the top we have some C-Sharp code that's setting the title for this page. Let's make this a welcome page instead of a homepage. We'll save that to Hot Reload. Yeah. Up there at the top, and then it says Welcome page. With.NET 6 we can Hot Reload changes into our CSHTML files, to Mark-up to tag helpers, and our C-Sharp code. In fact, this version of Hot Reload is using a new capability and the runtime which is reloadable types. In order to make this work seamlessly, we actually had to add a capability so that we could take the page type and swap it out at runtime with a new one so that you can see these changes seamlessly. We can also do CSS changes as well. CSS Hot Reload. Let's go find the main CSS style sheet for this application. Here it is, site.css and I'm just going to add a selector in here for the, lets do body actually, do body and we'll set the background color to red and hopefully that should work. Did I mistype something? I think that, let me rerun make sure that I got this right. Normally this should Hot Reload the, CSSH. That's working. Let's try blue. Maybe we lost our connection somehow, Save. I don't know what's going on there. I've got some issue with my machine, but you can make changes to your CSS styles and apply them to your running MVC and Razor Pages applications as well. All right. Well, that's, did I mistype anything and I didn't mistype anything. I'm not sure why that's not working. Let's look at that later. Also new in ASP.NET Core and.NET6 is you can take CSS styles and sculpt them to a particular page or view. Let's try that out. I'm going to go into the solution explorer. Actually let me get their app running again and let me remove the ugly background color that I added earlier so that doesn't look hideous and unreadable. This app has a couple of tabs, it's got the Home tab which we've been looking at, and then also has this privacy tab, and they both have H1 headers. On the homepage and Index.cshtml, it's got an H1. That would be that welcome to. Netconf text that we saw earlier. Then on the Privacy page, it's also got an H1, which is just rendering the privacy policy texts that's stored in the view data. I want to update the styles for that header, but just for the Privacy page. I'm going to right-click on the pages folder. I always accidentally click on the page itself, but you have to do it on the folder. Add a new item, and let's add a style sheet. We will name this a particular way, we're going to name this privacy.CSHTML.CSS. and we'll save that. You can see now it nested itself underneath our privacy. CSS HTML page. We can add a style in here and let's just do a broad selector, all H1s. We're going to try and set their color to pink. Now this one I know I do actually have to restart the app. This will not be Hot Reloaded. I don't think we have Hot Reload support yet for page or a view scoped styles. Yeah, that's something we'll have to look at adding in the future release, but we can go ahead and rebuild. Now notice that the heading on the homepage hasn't changed. It's still black. If we go to the Privacy page now. Yeah, and now that is Pink, so we are scoping that style just to that page. It won't affect any other pages in the application. This can be a convenient way to manage your CSS styles for your web apps. All right, next thing I want to show, let's take a look at async streaming. For this, I'm going to close down, this app. We'll go open up a different demo project. This one over here. Async streaming is the ability to take some data that you're fetching asynchronously like a collection of data and have those items stream down to the client without having to buffer them all into memory. Now, in earlier.NET releases, when you take some data and you want to serialize it down to the client as JSON, you had to pass it through one of the JSON serializers and the JSON serializers just didn't have built-in support for asyncronumerable, they just didn't have it yet, but that was added in.NET6. It's now available in the system texts JSON serializer and so we've wired that in now to our ASP.netcoreMVC, so that you can use this from your web applications. Now to show you what difference this makes, I actually have two versions of the same api. I've got a.NET5 version and a.NET6 version. Their controllers are the same. These are both API controllers, so they're both just generating a bunch of random weather forecast data and then there are logging out whenever they are sending a weather forecast down to the client and then they actually yield return the actual weather forecasts, that's the.NET5 version. The.NET6 version looks almost exactly the same. I think the only difference is it's using this new shared property on Random, which is this thread-safe property for getting access to a random number generator. Nice new feature in the.NET Framework libraries in.NET6. If we run these, Let's start with the.NET5 version. I'm going to, select that as the startup project. Now in.NET5, there was no way to asynchronously stream all the data down to the client, so before it even sends anything, it has to read all of the data into memory, collect it, and then give it to the serializer and so as you can see, the browser, it's just sitting there. It's not even doing anything. Let's just let that sit. Actually, let's check the output CR. We're sending weather forecasts. They're just not going anywhere. Let's try the.NET6 version. We'll switch, not that one, this one, and we'll get that started. Now watch how this renders. As those were enumerating over that Async Innumerable each of the weather forecasts instances, are being able to be sent down to the client. Let's see if we can tell where we're at. Yeah, we're almost there on the.NET5 version. It's about, I think I had 500 weather forecasts that I wanted to stream down and so supposedly when we hit 500, it should have finished buffering and then we should see it pop up on the left-hand side, and there it is. This is showing the benefit in terms of time but really the benefit here for async streaming is in terms of memory, instead of on your server having to allocate all that memory to send a big payload, a collection of items down to the client, you can just send them one at a time. You don't need to hydrate them all into memory all at once. That's async streaming with.NET6 and nice new efficiency gain. Let's go ahead and close these guys. Let's free up our ports and go back to here. Now if you're building single page apps with ASP.NET Core, like you have a Angular frontend maybe or a React frontend, there are some nice improvements in.Net 6 for those scenarios. First of all, we've updated our out-of-the-box templates for Angular and React with an ASP.NET Core backend to use the latest versions of those frameworks, Angular 12, Angular 17, at least the latest versions for now. We've also changed how those templates work. Now in previous versions of.NET, we actually had integrative framework support for each of these frontend JavaScript libraries. It's like we had specific framework APIs for integrating with Angular or integrating with React. We had basically had middleware that would orchestrate a proxying dance with their corresponding dev servers. The ASP.NET Core app would sit in the front, and it would then proxy requests to the Angular or React Dev server in the back, even though technically that was the frontend of the application. Then when you publish the app, all the static file assets were collected into your ASP.NET Core apps they could all be hosted as a single application. The problem with this is that, it required framework integration for every JavaScript framework that we wanted to support. If we wanted to do view or felt or whatever the new hotness is and the JavaScript World, while you were waiting on us to provide that support out of the box. In.NET 6, we've completely flipped it around. We now leverage the proxying support in the development servers for these frontend JavaScript Frameworks, which pretty much all of them have. You stick the JavaScript to development server in the front where it belongs and it proxies requests for API calls to the ASP.NET Core backend. This is all done just with some simple orchestration in the project template and it scales much more naturally to other JavaScript frameworks. If you want to add a view template, you can just basically use our existing templates as a pattern, copy and paste some code and you have got an ASP.NET Core and view application. We think hopefully this will create a much nicer community of spa templates and people using different JavaScript frameworks with Asp.Net Core. Now Javier Cavallaro Nelson actually has a whole talk on Asp.Net Core spa support that is, I think on the third day of the conference early in the morning, so be sure to go and check that out. Blazor, of course, has a whole bunch of new features in.NET 6. We covered a bunch of these features earlier today, if you stay tuned after this talk, we'll talk about the Blazor improvements specifically for component library authoring and show you, all the goodness that we have there. There's also another talk tomorrow by Steve Sanderson on the WebAssembly improvements in ASP.NET Core and Blazor and.NET 6. Now, there are lots of other ASP.NET Core improvements that don't fit specifically in any of those themes. We now have HTTP 3 preview support in ASP.NET Core and.NET 6. HP 3 is the latest version of HTTP, and it has a completely new UDP based transport called QUIC and addresses a head of line blocking issues that could occur with HP 2. Please give it a try and let us know how it's working for you. Whenever we introduce a new HP protocol implementation, we take time to make sure it's hardened and in good shape before we actually stabilize it. HP 3 as well on its way and you can help us with that last mile. We also introduced a bunch of low-level APIs so that you can have control over how sockets are created and handled in ASP.NET Core apps. This can be useful for low-level TCP related scenarios. We reworked the APIs for dealing with HP headers in Asp.Net Core so that they're now nicely strongly typed. For folks that are deploying ASP.NET Core apps to IS space-based servers, we now have a form of shadow copying based deployment available for you to use, if that's been an issue for you. If you're not familiar with shadow copying. In some cases, the application files can be locked while they're executing. Shadow copying is a technique to actually be able to deploy while the site is still live. If that's not an issue that you're currently hitting, you don't need to worry about it, but if you are, then there is now a feature in.NET 6 that you can leverage. We've added support for HTTP and W3C logging in the stack. We have a new signalR C++ client and then gRPC has a bunch of new capabilities for retries and load balancing that I believe James Newton-king will be talking about, I think it's later today. Really quick, let me show you the new HTTP login because it's fun. I'm going to go over here to Visual Studio and let's do File New Project and what we're going to do is we're going to create an API, ASP.NET Core web API and click ''Next''. Let's call this, I don't know, HTTP logging demo and then for this one I want to make sure I'm using the new mini API patterns so I'm going to leave this unchecked. I don't want controllers for this one and.NET 6 looks great. Let's go ahead and create that. To wire up HTTP logging, it's actually really simple. We can just go into program CS, nice and simple and clean with the new mini API model and then what I'm going to do in here is up at the top of my HTTP request handling pipeline, I'm going to app.UseHTTPLogging and this will then log details about all the requests and responses that are being sent to my application. There's one other thing I need to do which is set the log level that I want to use for my application. By default in development, all the ASP.NET Core related logging has set the warning, we're going to need an information level for our HTTP logging. I'm going to set this here, Microsoft.AspNetCore.HTTPLogging and we're want to set that to information. Great. I think that's all we got to do. Now if we run this and then look at the logging output in the console, this is just a simple API endpoint, you can see we're getting all these log entries now where we're getting details about the requests and the responses. By default, you get a property bag and information about the requests. If you want more, you can, of course, configure to add more, by going into the configure services method and switching the options for your HTTP logging. Nice new feature can help you troubleshoot some of your, if you're having HTTP level issues with your APIs or endpoints. With that, make sure you check out all the ASP.NET Core talks at.Netconf. Right after this talk, we're going to talk about next generation Blazor components. If you missed the previous Blazor talk or the mini API talk, be sure to go check those out. Sourabh Shirhatti, is going to be talking about diagnostics and observability for ASP.NET Core apps. That's tomorrow and James Newton-king's gRPC talk is also tomorrow. Be sure to check those out as well. Sebastien Ros is our performance expert for ASP.NET Core and he's going to be talking about how we benchmark ASP.NET Core apps using the.NET Crank benchmarking tool. Then Javier's talk on spot applications will be on day 3 early in the morning. Thanks for listening and with that, I'm happy to turn it over for a few questions. >> Cool. Thank you very much, Dan. Appreciate you. We're going to actually spend more time with you, as you pointed out, we're going to bring Javier in in a few minutes here, we've got a couple of questions. >> While we're doing this. >> Couple of questions here. Complement here from Turtleonabike says great to templates using Bootstrap 5.1. Will Scaffolded items like identity also use Bootstrap as well? >> That's a great question. I didn't show that, but yes, they will. Yes, Scaffolding, will use them. Any place where we generate code that needs to integrate it with your app will use the latest Bootstrap version. >> Let's look at one that's a little bit critical. Folks are saying that they felt like minimal APIs and things like that took Controller-structured web APIs and turn them into what they call a spaghetti code of Lambdas and they feel like it might be good for hello World, but if you need authentication, databases, and other custom services, it's badly structured. What do you say to people who feel like we've moved their cheese? >> I think in some sense that's fair. If you're going to be building a lot of endpoints, you're going to want some structure to your project so you can manage all of that code and controllers are still a great way to do that and there's nothing wrong with a good API controller and using those for large-scale applications. Where I think the new mini API model really shines is if you're new, like you've never built an ASP.NET Core app before and you just want to get something up and running. With this new model, few lines of code, you've got your first API endpoint. You can start adding those map posts in line in a single file and that's good for growing up and then maybe at some point when you're going to build something big, maybe you do move to a controller. There are patterns, of course, where you can take that code and separate it out into other C-sharp files so that it's not all just in one file. I mean they're just effectively method calls so there's nothing that prevents you from doing that and I expect that people will start to develop those conventions around this pattern. >> Correct me if I'm wrong, but I feel like all of this is just an extension of that ASP.NET Core pipeline and that people sometimes forget, it's not a competition between MVC and razor pages. I remember when razor pages are what we call the web pages came out and later razor pages people were like, what if I want to use both? Well then use both. If you want to use all three, like you hook the pipeline up anyway you want, can't you? >> You can have API controllers and mini APIs inside the same application. Sometimes it's just simpler to put a single method call in there to set up an endpoint when you just need something very simple. They have their time and place. >> Then I don't understand this question. You'll have to explain to me here. They're saying Daniel can one import RCL pages and components at runtime and Blazor? Mark them as I don't know what RCL is? >> RCL would refer to, I'm pretty sure, Razor Class Libraries. That's the ability to have a dominant class library that has razor files in it and you're using the razor SDK to build those into normal.NET classes. In Razor Class Libraries you can place your components, you can put MVC views, you can put razor pages. This sounds like it's asking, can I load those dynamically? >> They want to make a plug-in architecture and I added it because they said to hoping Scott will notice this. >> I mean, there are absolutely other frameworks that are built on top of ASP.NET Core to add modularity and Multi-tenancy. Orchard core is one that I know of that does a really good job of this. These frameworks add that level of dynamic loading of components into the application. For Blazor there's also one, this one called octane, does something very similar where you dynamically load Blazor components into the application. Can you do it? Yes. Is it something that I would recommend everyone doing for any application they're building? Probably not. You probably go look for a framework that already has that setup for you. There are complexities with doing that well. I would look at the ones that are established. >> Cool.
Info
Channel: dotNET
Views: 16,312
Rating: undefined out of 5
Keywords: .NET
Id: UFXfmgQm_D4
Channel Id: undefined
Length: 29min 55sec (1795 seconds)
Published: Wed Nov 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.