Meet the new Asp.net core
ASP.NET Core had a very troubled and confusing genesis, changing names and merging with other projects in the course of the last 6 months.
In this talk we are going to clear up all the confusion and show what ASP.NET Core brings to the table for web developers.
- Understand what ASP.NET Core is and how to install it
- Understand the new flow of ASP.NET Core sites
- Learn what the default modules do
- How to develop outside of Visual Studio
View transcript
Okay, so it's 45, we can start. Someone sitting there, there are some, standing at the bottom, there are still some seats in front, so if you want to come in front. So, this afternoon we are going to talk about Project K. Does anyone know anything about Project K? No, because it's not called Project K. It's been renamed to VNEXT a few years ago. Actually, it was ASP.NET 5 until a few months ago, and now it's the final name, hopefully. It's ASP.NET Core. So it's about ASP.NET Core today. So, my name is Simone Chiaretta, I'm working on Umbracosi 2010, like say on and off. I'm a web developer since 20 years, which sounds like a long time. And I do triathlons, and I will do one this Sunday in Copenhagen. And I wrote, you might know me from this ugly book with these ugly faces on the cover, about ASP.NET MVC 1. Actually, I made a mistake again to write another book, which is called Front-end Development with ASP.NET MVC 6 AngularJS and Bootstrap, which has been recently renamed to Front -end Development with ASP.NET Core 1 MVC and AngularJS and Bootstrap as well. So, a lot of names have changed. But, what's the name? So, what are we going to talk about today? So, we'll see first what is ASP.NET Core, how to get started with an ASP.NET Core, which is kind of easy, so probably just a few seconds. And then we will see how an ASP.NET Core application is structured, which are the servers you can use in ASP.NET Core. And then we will do some demo, mostly the talk will be a huge demo. And then if you have time left, we will see how to build a console application that runs cross-platform. And how to develop outside of Visual Studio. And something about the new thing, which is the .NET Core, .NET CLI. And still something about Project JSON, which will go away, but for the sake of it, we can see how it is now. So, what's ASP.NET Core? So, ASP.NET Core is basically a new version of ASP.NET that instead of running on top of the standard framework, it runs on top of the standard framework. So this has the standard framework and runs on top of .NET Core. And the difference is that the .NET Core is just a subset of ASP.NET. And is supposed to run, as you can see from this diagram on all the systems, so Windows, Mac and Linux. So you have the choice, if you want to run on Windows, you can run on both the standard framework or the .NET Core framework. But if you run on Mac and Linus, you can only use .NET Core. of the .NET framework 4.6, you have ASP.NET 4.6 and you have ASP.NET MVC 5, but also well forms, if you're still using them. But on .NET Core, you don't have well form, you only have ASP.NET Core, which is this thing here. And then on top of that, you have ASP.NET Core MVC V1. And they are renaming everything to V1 just to avoid messing up with names. And so the entity framework was supposed to be Entity Framework 7 and now it became ASP.NET Core Entity Framework V1. So it's kind of difficult to get all the naming right. So, oh, it doesn't work this one. Okay. So how to get started? It's very easy. You just install Visual Studio 2015, even the Community Edition, and it runs very fine. It just automatically installs all the packages that you need. Or you can run Visual Studio code and on that also to install the package and also the framework that you need to install. And you can get everything from this .NET short URL that Microsoft managed to buy. And it just redirects to microsoft.com.net, I think. And from there, you can also download tools to build mobile application, which is Xamarin, and to build desktop application with this universal Windows platform thing. Okay. So let's see what is an ASP.NET Core. Basically, it's, I don't know, anybody knows anything about OWIN? Good. Well, ASP.NET Core is basically OWIN, an OWIN host. So everything that you know about OWIN, it's 95% will be the same on ASP.NET Core. So you will have the startup class, and inside the startup class, you have the configure method. And which you can configure your pipeline through middlewares. And something that OWIN didn't tell you, you can also configure the services because by default, ASP.NET Core is dependent injection aware for everything, so you can inject everything you want. But you have to configure upfront what do you want to inject into. If you don't know anything about OWIN, I wrote also a book, which this time is free. It's not a... I mean, you can download it from this Syncfusion resources file. You just have to register, but it's free. It's about 100 pages about OWIN. So let's go to the demo. No, before going to the demo, one more thing. We will stay at the end of the demo. You can run ASP.NET Core on IIS, but you can also run it outside of IIS if you're running in Windows. But also if you run on Mac and Linux, you can run it running on top of kestrel which is uh based on libv of course when you will run on kestrel kestrel is not a full-blown server like iis so if you're running on on a kestrel you will have to put it behind something more powerful like nginx or apache or anything that you like to so let's go to the demo hopefully it will work okay so if you want to start a new project esp net core it's pretty easy you just go file new project okay so how do we do that no ask okay so let's go back to this one so file now you see it okay file new project and then you choose asp net core web application on dot net core you can also choose to run it on net framework but then well it will use all the framework that you know already so and then when you can you can choose to run you have three templates one it's an empty file one it's web api and one it's a web application i will show you how it's the web application is done because it takes a while to create and in the meanwhile i hope i start the empty and i tell you why i start with the empty not with the full one the problem of starting with the full template is that with asp net core you get huge amount of new concepts if you that you were probably not aware of you have gulp files you have bower you have npm and and you have you can you have this uh you said a task runner where you can run gulp the project and you have Bower somewhere else in the dependencies. You have NPM, and you have a huge configuration file for ASP.NET where you can configure identities, servers, mail sending thing, and a lot of... So it's very difficult, if you don't know anything, to get started directly with that. And also to make it more explicit that with OOIN you have to opt-in, we'll start with the empty application. So we can close this. So this is the application configuration file, which is a startup class, which contains, as I said, the configure method and the configure service method. The configure method contains all the middleware that will run, so it defines the pipeline in which your application will be executed. So, for example, you can say at the beginning I do some user authentication, then I do some logging. Then I do some other, I don't know, error handling. And then at the end, you go to the bottom, and every middleware passes the same information to the farther middleware in the chain. And then at the end, eventually, your request is executed, for example, by a real framework like MVC or Nancy. Or if you want to do it yourself, you can run it also like this. And you can just handle the code yourself. And then it goes back in the same way it came. Middleware can be as input or output or both. So once it enters from authentication, logging, blah, blah, blah, and reaches the end, it executes, and then the response is sent back and goes back to the same pipeline, of course, in the opposite direction. So if you just run this thing, it will... My computer is a bit slow, so we'll have some wait time. It will compile. And then we will see... We will see a very fancy text output in the browser, which will say, hello, world. And in this case, it's running on IIS Express, hosted inside... And here we have... It's hello, world, and it's a text. It doesn't have any HTML or anything. So let's try to do something more useful. And add MVC. So how do we do to add MVC? So first we comment out this stuff. So we just add... As middleware, we use... We say app, which is the application builder, which you add all the pipelines that you want. And you do use MVC. So you don't see it here, because, of course, it's a... It's a... One thing that I probably forgot to tell, that you have to opt in in everything you want in ASP.NET Core. So... And ASP.NET Core is super modular. It's not like before that you had ASP.NET and you had everything installed. In this case, you have to configure each package you want. And to do that, you have to go... Well, sometimes Visual Studio suggests which package is good for you. But unfortunately, for ASP.NET MVC, it suggests you do the wrong package. So... You have to go to Project.JSON. And you just say that you want to add... It's basically like if you know Bower or NPM. It's the same. You just put all your dependencies here. But luckily, with Visual Studio, you have IntelliSense. So you can say that you want a Microsoft... Why it disappeared? A Microsoft.ASP.NET Core. .envys... .envys... .envys... .envys... .envys... And then it also suggests the version that you can use, which is in this case, ASC2. So you save. And it automatically restores the package if the god of the Wi-Fi helps us. And it takes everything down from NuGet. So... Okay. And then another thing that you need to do... And now the red swiggle went away because... It recognized US MVC as... as... as metered. Another thing you have to do for MVC... MVC is very dependent on dependency injection. And so you have to configure also that you want to use ASP.NET MVC as... You have to... You want to register all the services that you need in ASP.NET MVC. So if you run... If I run like this, nothing happens because, as you know, ASP.NET MVC needs controllers, needs view. So I will have to create everything manually, but I made it up front already. So we just copy everything that I did here. And we copy it back on top of the other one, which is this one. And voila. We have everything running. Yes, well... So I also added something that... So I don't have to type anymore later. So what we have now... I just use a slightly different US MVC statement. I want to use the default route, which is having a controller and... and metered. And the ID at the end. Otherwise, I would have to configure manually inside the US MVC, which route I want. So... And here we have add MVC. On top of that, we have the controllers, which is a simple controller that... like, it just renders the view. And then I did some more controller for... for later. And then the view, it's pretty simple. It just... renders the HelloCodeGarden. So let's run it and see what happens. Everybody okay so far? Good. So... So... So... So now it takes a bit longer to run because it has a framework. It has to initialize the dependency injection and it has to initialize all the MVC pipeline as well. But eventually we'll write something. So we have HelloCodeGarden. In this case, HelloCodeGarden runs from the view. So we went... We went through the controller, the dependency injection that injected the controller and the view engine and all this stuff that it does normally. And then at the end it runs the... the... renders the view. So, but... As you can see here, we have a few problems. First of all, in the... in the view, I have a CSS file which is not recognized. Even if it's here in the... in the wwwroot CSS style. Then we have another problem, which I show you immediately. If I go to a root that doesn't exist, like a... foo... I go to the hello world. I have no idea why I go to hello world, but... So... Okay. Maybe I left the thing inside. Oh yeah, I left the hello world. So basically it didn't find anything in the... in the... as controller. So it went outside the NVC, and then the last piece of the pipeline was this up-run return-hello-world. But... Okay. If you didn't have this, you would see an empty page. So just for... sake of the demo, let's... Let's comment it and run again. So... If you don't... That's why we get this. Because we didn't configure anything about error handling or about serving static files. Because the CSS... Now when the browser requests localhosts slash styles slash style.cs it tries to resolve it as a controller with its own action. So... But we don't have any of this, so either we implement the controller action that returns the CSS, or we do something else, which is what we are going to do in a while. Which is... serving static files. Which you think that the web server should serve static files by default, but maybe you don't need it, because you just render a web API. You don't need static files. So let's do the demo again. And we run... foo. And we get an empty page. If we run... another thing, another page I've done, which is home... Oh, come on. If we... by any chance we encounter an error... Let's... stop this one. You also get an empty page. So how can we fix this? First of all, we have to add a few packages. So... if I go back to the startup.cs... I can uncomment this file. So I say I want to use a status code page, which handles... No, not this one. Oh, come on. And we... also want to uncomment this one. To see error when they happen in a production. Of course, they are not there yet, at the moment, so we have to install... the package. And in this case, Visual Studio is smart enough and tells you, oh, you have two... installed two of these, you want to install something? So it tells you... Suggests you to install the actual development... debugging experience for... for a... ASP.NET Core. So it's restoring the package. And I'll show you later at the end how many packages we are going to add, because it's a huge number of packages that we need to add. So now if we run it again... we will see some kind of 404 page, and some nicer version of the yellow page of that, that we are used to. So it will open... OK. Sorry about the computer, it's a bit slow. OK. So here you have a yellow code garden, and if we go to... Foo... we have a 404 page. We can make it better, we can make it look beautiful. This is the default one that you get, but still you understand that it's a 404, it's not like a white page that you don't know what the hell it was. And if we launch again the error, throw again the error, you will see this nice... developer... developer page, developer exception page, which shows you the stack like it was before, in the yellow page of that, but you can also see better like... if you had some query parameter passed by, you have some cookies, and all the others that you have. Which is pretty cool. But now let's try to solve... some... another problem. We don't want this page to... to be shown to everyone, because... I mean, in production you don't want to... this... this specific developer page. So what we are going to do, which... With ASP.NET Core it's super easy to understand in which environment you are in. To do that... you just have to... pass to the configures method... this iosting environment. Basically you can pass to the configuring method whatever you want, because it's also... the... the IOC... let's say, aware. So... every time you find an interface, you try to resolve it somehow. And if you pass something that is... core from the framework, you will inject it... anyway. So... And with this... you have this nice method that you can use. Is development... or is production... or is testing... or you can also do a named version... maybe you have acceptance... you can do... I mean... go crazy as you want. Or you can just use... production, development, and testing, which are the default ones. So we can... Okay, uncomment... And... So... what we are going to do... if you are a developer in development environment... you see this... runtime information page, because maybe you want to debug and understand which version you are running. And... we use this other... middleware, which is called... exception handler, which redirects to another page. But... since it's so long to build stuff... I will just... skip the demo of the development environment, and just go directly to the production environment. what do we change from production to development to production, actually? It's... It's an environmental variable. So... In... When you do it in... in Visual Studio, you just go to... project... application... project configuration and debug... and it will eventually open. But if you run it in IIS, you can just configure the environmental variable. Or if you run... in Linux or Mac, when you run the command line, that will start your web service, you can just pass... environmental variable... to the command line. Okay? And the... And the environmental variable is called... Okay? Not responding... Oh, come on! Anyway... So, any questions while we wait for the thing to go back to life? Okay. So there is this IIS pinet core environment... Which is the name of the environmental variable? I don't know why it went smaller... Oh, come on! And you can change it to production. Production. Okay. You save. You launch again. Sorry? Where is what? I just said... I just stored it in the browser. You can see it in the launcher of Visual Studio. So it's just an environmental variable that Visual Studio will use then to set when it runs in the bug mode. It's not some... It's not in the project, it's just... It's stored in the CS project, like everything that you do in... Well, actually, now it's Xproj, but it will go back to CSproj in the future, and we get rid of project JSON, and we go back to MSBuild, but that's something... Changes. The IIS pinet team likes changes. So if we run again this page with the error, you see that an error happened. So you can choose easily which environment you want to run. So the other problem we encountered was that we didn't see any CSS file. So if we stop it again, and we go to the startup... Okay. So we uncomment this line. And this... This tells that you have to serve static files. So by configuration, by default... Of course, we don't have the package, so we have to install it as well. And by default... Static files... Static files serve files from this www... Route. But you can change, I mean, to another folder. You can have multiple static file routes that you can have. You can also run embedded... An embedded file system. Maybe you want to build your own, how do you say, small application that also hosts. A small website for configuration purposes, maybe a Windows service. And then, in this way, you can have your own embedded web service. And you don't have to have files spread around because they will all be inside the embedded file. And the resource file. So... We just run it again. And the error page will become prettier. Well, I'm not a very good CSS guy, so... Probably if I was not giving this session, I would have been in the how to be front-ender. But... So... Well, that's it. So that's, I guess, I hope, raised the awareness of the fact that with ASP.NET Core, everything is in a hop-in. Even the smallest thing that you will give for granted, which is like running a... I mean, returning a CSS file, how difficult would it be? You see, now it's much nicer. Different font. Wow! Nice error message. And... So everything is... You have to opt-in and you have to choose... This can be, at the beginning, a bit frustrating because... You say, come on, I just want a CSS file. Why do I have to add something? But then, in the long term, if you were stuck to the way like ASP.NET... Standard ASP.NET, let's call it standard. You're used to. You have everything. You have... Connection to database. Connection to all kinds of identity provider. And everything is there. And you have a huge surface. Well, in this case, you have very small packages. If you just want to run MVC without any weird connection, you just have two packages and it works. Okay. Let's go on a bit. And let's see a few other interesting features. Actually, we only have 10 minutes. 10 minutes-ish left. So I ask you... There are two things I could show. One is showing logging and configuration in ASP.NET which are kind of coming out of the box. Or otherwise, I could show how to build a console application using Visual Studio Code that runs as a... Let's see. Eventually will run on cross-platform. Okay. I'll just show both, but without demo. So... So... This is how you implement the logger. I will not run because it takes forever to run the demo. But here you see you can inject the logger factory which comes out of ASP.NET Core out of the box. And then you say, I want to create logger. You say you want to run it on the console. The console makes sense in this case because... Okay. IIS doesn't have a console. But if you want, you can run on web app one which will run this program. CS file. Which is basically a console application. So also now from ASP.NET Core, RC2 which is the current version we have now. Also web applications are console application at the end. You can just run them and it will be self-hosted. And if you happen to run it IIS, we just pick the startup part but everything else will stay inside this. So if you run launch from this, you can log to the console. And of course here you see the wiggly red thing because we don't have the console. And then configuration also is very powerful. I think it's one of the nicest features of ASP.NET Core. Okay. Because it's hierarchical. So at the beginning of the application, you say, I want an application builder. And you tell him, I want first a JSON file. Then I want another JSON file which is called as the name of the environment. And this overrides the first one. So if you set something in the base one. The second one will override the first one. And at the end you can also say, I want an environmental variable. And then this environmental variable will override again all the configuration you've done. This is super cool because, for example, in your local environment, you do something. Then you go to Azure and you want to just override something because it's Azure. You just put them in the Azure panel as an environmental variable. And that's pretty easy. And JSON are like this, are JSON files. So the example would have written, Buongiorno ASP.NET Core. And you could change Buongiorno to Hello or this kind of stuff. And to reference it, it's kind of easy as well. You just say, I want... I just get the configuration. And then inside the configuration, I just reference the JSON file. And the properties. And if you add an SD, you just reference that with a column. Okay, something more interesting. Because it's fairly new. So we just build an application now with a command line. So first we make a folder. We'll call it LOCG1. Sorry? Ah, yeah, okay. Thank you. Then we move to this LO folder. And then we use this new command line interface, which is .NET. And this creates a new project, which will just contain a project JSON file and a startup. And then we do .NET restore. Come on. Which will go to NuGet and will download all the packages that are referred in the JSON file. I had already downloaded them, so it just checks if they're changing. And then it just takes them from the local file system, like NPM does. And then finally, we can run it. .NET run. So it will compile. And then it will say hello world. And that's it. And if you want, you can also run from Visual Studio Code. And in Visual Studio Code, you also have debugging now, finally. But I won't show because we're kind of already late. So I'll wrap up the... I brought a blog post on my blog about how to configure this stuff because it's not super easy. You have to pull down some packages and so on and so on. So... Let's bring back the slides. So these are the web services I've seen before. Okay, console application. So with the .NET command line, you can create a new project. You can restore packages. You can run the thing. If you call .NET build, it will create and it will compile the IL like it did in run, but without running. And then if you configure properly the package, it will also build... That is the super cool thing about .NET runtime of this .NET command library thing. It will also compile as native application. So if you're on Windows, you can create a .exe file. And you can move it to another computer which doesn't have .NET Core. And if we run like in any other application. And then you can also run for... You can also target Mac and Linux. Just a quick look at the project JSON. In the project JSON, which I said will likely go away. Because now that this Core framework is in the... Core framework. This framework is going to be used also not only for ASP.NET. But it will also be used by Xamarin. By Universal Windows platform apps. And all this kind of stuff. It's impossible to remove the CS project. Because otherwise all these other people that don't know anything about NPM and Grant and Bower. They will also start to understand JSON. Which they don't want. So we just go back to CS project. But basically in the CS project, you can specify some metadata. All your dependencies. The framework you want to target. And even more you can add additional commands. I mean, basically you can configure everything that you can do also in NPM. Also you can do... When you install it also can run NPM and Bower. And get all the dependencies and all this fancy stuff. But, okay. If you do something now, you have to kind of learn it. But probably in September or later will go away. Actually that's the topic of the next slide. So... What are we talking about now? Which version are we talking about? The framework is RC2. Which means that it's fairly done. And yesterday Shannon talked about the RC3. Which will probably be the RTM. And then RC4 and RC5 which are future updates of the framework. But the thing that is kind of weird. That it's not everything is RC2. Because the tooling. The .NET command library interface. And all this package thing. And all the dependencies. Are in preview 1. Which means it's an alpha. It's not even a beta. It will RTM with RC3. Probably at the end of June. Or maybe July. You never know. And in that case it will be preview 2. But the tooling will go RTM. Only when Vita Studio version 15. Which will be probably called Vita Studio 2017. Will become a thing. So probably, I don't know. End of the year. Or probably they usually release around November. And it will be 2017. Because November 16. It's part of fiscal year 2017. So it will become 2017. And that's it. So I hope you understood what SPNet is about. Not that you really need to know to work on Umbraco for the moment. But in the future it will be there. So it's important to know what is the foundation of SPNet. Of Umbraco V9 or V10. Or what probably will be called Umbraco Core V1 as well. But yeah, you never know. And that's it. If you have any... This is Twitter account. If you want to contact me or ask questions. And I will put on my blog post a link to the repository of GitHub. And the slides. So if you have any questions. Good. Can you pass in this? Thank you. I remember in ASP. I remember in ASP.Net VNext you had the Roslyn compiler. Sorry? The Roslyn compiler was in ASP.Net VNext. Yeah. Roslyn has been released already. Yeah. Is the Roslyn compiler part of the ASP.Net Core as well? Yeah. Roslyn works on ASP.Net Core. Okay. And it's already been released as RTM a few... Some time ago. Yeah. Okay. I was just wondering if it was part of the... Yeah. Yeah. All this thing that the compilation run on Roslyn. Okay. Okay. Does it work this thing? Anyone else? Just give it a go. But I don't think it will work. Okay. Did you say it has the DI container built in? It's got it. Yes. It's just... Yes. Okay. Fair enough. So it's not any of the usual containers. It's a thing that they built. But you can... I think you can swap and use whatever you prefer. How do you configure it? I have no idea. Fair enough. Well, in theory, everything is configurable. So probably there is a way to use something else other than what they use. But... Anyone else? Yeah. Okay. Then it's... Rev up. Thank you. Hello. Hello. Hello. Hello. Hello. Hello. Hello. Hello. Hello. It works. You just have to almost swallow the next one. Yeah. Thank you. Looking forward to trying it. Thank you. Thank you. I wanted to avoid all this commenting thing. So what you've done is really cool. So I tried to do yesterday evening, but it was too late to go to this new tool. So basically, you just do commit, and then just tap on the finger to commit. Yeah. But then how do you play that? With Git extensions... Okay. ...and Git in the basic Git tool. Okay. You can just reset the branch to any given commit. Thank you very much. We just go reason French to here, and then everything rewinds. Thank you. Thank you. Thank you.