.NET 6 Minimal API [Create a GET API with only 4 lines of code (.NET 6 and C# 10)]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to dot net core central last week c sharp 10 and dot net 6 was released during the dot net conference and one of the feature which got a lot of attention was the minimal api in dotnet 6. so in today's video i am going to walk through what is minimal api how to create them and what are some of the features in c sharp language that was added which made it possible to build something like minimal api so minimal api in a nutshell is a new way of writing the api with minimal code now that doesn't mean we have to absolutely use that the previous way of creating controllers and using controllers are still there so if you are not comfortable using minimal api you can still use the dotnet 5 way of creating a controller and injecting your dependency and use it that way but if you are building a quick prototype or you are creating a nano service i would say an extremely small service responsible for doing just one simple thing you can use the minimal api for that some other languages like node.js with express already supports building apis like that with three to four lines of code and in today's video i'm going to show how to create an api with just four lines of code and then improve that to use some of the advanced features so for that what i'm going to do is i'm going to create a console application i'm going to name it as minimal api.demo and here as you can see i installed visual studio 2020 visual studio 2020 is what provides support for dotnet 6 out of box and it has changed some of its user experience for example here now you can select the version of dotnet which will tell what type of version it is for example 2.1 is out of support three one is long term five is the current six is also long term support so i'm going to continue using dotnet 6 and create the console application once the application is created you will see that it comes with a single line which is console.writeline now this is nothing new this is something which was introduced in dotnet 5 where you do not need to have a program class and main method inside the program.cs you can just write code and the compiler will understand that it is everything is inside the main method so this is the same concept which is carried forward though one thing which is different is you don't see any using statement here the reason for that is with dotnet 6 there is a concept of implicit using and by default implicit using is enabled which means for most of the common using statement we don't have to write them the compiler will automatically add those so that's is a very handy feature will minimize the number of using statement we have to use and the other thing you'll see that the nullable now is by default enabled in dot net six which was not the case for dot net five we had to manually enable it in dot net five so these are couple of small feature changes whereas this is very impactful and a new one so let's get back here i'm going to delete everything so as i said before i start working on the minimal api there are a couple of feature i wanted to talk about which are needed for minimal api and both the features are related to lambda for lambda whenever we have to declare a lambda we have to use the type of the delegate during declaration so for example if we have to have a functions which takes an integer and returns a boolean we have to declare it with the func and here it will take an integer and return a book and then let's say this is a function which is going to find out if a number is greater than 10. so it's greater than 10 and then we can define this function and as you can see the intellisense has significantly improved in this new version of visual studio there's a lot of suggestion which is showing up which did not used to be the case for previous version so here what we can do is we can say value x and as you can see it just automatically shows you i can just do a tab and if greater than 10 so i'm just going to say it's greater than 10. just based on the name it identified that i'm doing a greater than expression and it just created it this is pretty cool so you can see that now in the c sharp 10 what we can do is we can explicitly define the type of the parameter so i can do like this and when i explicitly define the type of the parameter i do not have to use this delegate anymore i can just say var and when i say var you can see that the compiler has automatically inferred the type of the delegate and we did not have to if i get rid of this it's going to throw an error saying it cannot infer the type but if i put this it can infer the type and this is something which will be one of the crucial thing that we need when we start using minimal api and the next thing is also now we can define a return type for the lambda in the expression itself so this is one of the feature which will be used when creating minimal api so it is important that we talk about it the next feature is in all the previous version of c sharp and dot net we could use an attribute only with class or method we could never use it with lambda in this version of c sharp we can use attribute in the lambda expression and this is also one of the feature which will be heavily used when creating minimal api so for that let me create a new attribute class and let me name it as test attribute and once i do that there is another important thing you'll see is that once i say colon it automatically understands that i am trying to derive from the attribute just because i named it as attribute so the artificial intelligence for intellisense has been improved significantly i'm going to get rid of this namespaces now another feature which is not related to the minimal api but extremely powerful is now the class do not have to be inside curly braces for the namespace the namespace can be declared at a file level like this and then we can get rid of the curly braces and that would save a lot of real estate in the horizontal of the class and i think it's pretty important and helpful so let me get it to the next line so now what i'm going to do is i'm going to declare an action delegate here and let's say it takes a string and i'm going to name it as action and it's going to take the string input here and it will just do console.writeline of x and now for this particular x i can add an attribute so i can add the test attribute here and i'll have to add the namespace and now i can add the test attribute here now another cool feature which is also added as a part of dotnet 6 is that namespaces do not have to be declared in every class apart from having the implicit namespace the namespace which are not added implicitly can be added in a separate class as global namespaces and every class will automatically get those so if i create a new class here and i call it as global namespaces and i can just copy this one here and add a global in front of it and now i don't have to declare the name space here so now what i can do is i can go ahead and call the action and pass hello world to it we should print hello and then i can do console dot write line and call the is greater than 10 and pass let's say 11 to it and now if i run this application it should print hello world and then true as a part of the response yeah now let's go ahead and create the minimal api and for creating the minimal api i'm going to change the sdk here from microsoft.net.sdk to microsoft.net dot sdk dot web i'm going to save it and once i save it detected that i changed it so it wants me to reload the project so i'm just going to go ahead and reload the project and once the project is reloaded you'll see that the properties is added which has the launch setting and in dependency it would add the asp.netcode.app as a framework so now we can go ahead and start writing the minimal api and for that as i said we could write a minimal api with four lines of code so let's go ahead and do that first thing we'll do is we'll declare a builder just like we used to do in the previous version but we will use web application dot create builder and pass the arguments which is the arguments part of the main method which is not visible anymore but it is magically injected by the framework so we call that and then i can say bar f equal to builder dot build and then app.run but before f dot run i'm going to say app dot i need to declare minimal api so this is where i'm going to declare the minimal api and for that i'm going to use the method map get and if i do the map get you can see that it takes string pattern for the url and delegate handler so for the string pattern i'm going to say slash api slash user and then for the delegate i can just create a simple delegate and declare an object which i'm going to return that's about it as i said just full line of code and now we have an api ready and if i go ahead and select this and run the application and go to slash api slash user i can see id and name showing up as expected as you can see just four lines of code and we have our api ready but this api is not very useful of course because this is not how we're going to do anything we'll probably be having a class which is going to get the data from a database and it is going to return so let's create a class called user provider but for the simplicity and time constraint i'm not going to get into database and do anything right now i'm just going to return static data from that class so i'm going to create a user provider it is going to be it's going to have a public user array and get and let's create the user type and for the user type let's provide id string name and let's get rid of this to save some space and same here we can just drop this okay and now here what we can do is we can just do return new and for the user we can say id is equal to one name is equal to java that's it for the timing and let's create an interface for the user provider so now what we want to do is we want to use the i user provider instead of doing this so let's see how we are going to do it so first what we have to do is we have to add the i user into dependency injection container so for that we'll do builder dot services which is going to give the iservice collection and then dot add single turn just like we do usually we can do that and here i can say i user provider and user provider so now we added this to dependency injection now the goal is to pass it along here so how we are going to do that for that there are a couple of options one option is you can just use i user provider provider and the framework will kind of do its magic and say okay we're asking for i user provider let me see if i have it in my dependency injection container if i have i'm just going to give it to you from there or you can explicitly define an attribute here from service and it will and i have to add the namespace for sp.net core mvc and now it will know that it has to get it from dependency injection container and here i can just say userprovider.cat that's about it and this one let's move it to the global namespace and declare it as global and that's it now we created a dependency injected it and we are using it as a part of the user so let's run this and now here we try again slash api slash user and we can see we have this now let's say there is another feature which is for visual studio not for the language as such but there is a concept of heart reload this is where you don't have to restart the whole program so if we let's say have to add another user to the user array here we can just do it using this and we can give the user ids to and let's change now instead of restarting we can just do hot reload and we can just refresh this and we can see it showed up as you can see how fast it was to do a hot reload and show it in the ui so which is very cool so that is a very easy way to create minimal api now the other question you might have is okay we saw the get what about post and how about reading the data from the post so let's try that out for post let's create a user creator so let's create a new class call it as user creator and for the user creator let's create public bull create user user and here for the time being i'm just going to do console dot writeline very simple and we don't need this because it's part of the global and then i'll just return true and then let's go ahead and create a namespace now what we're going to do is we're going to come here and add the user creator in the dependency injection created the user we added the user creator to dependency injection and now just like map get i can do app dot map post and here i can see i have the same api slash user and then i'm going to create the i'm going to create the delegate but here what i need to do is first of all i need the same thing i need from service and i user creator and uh prompt services is a creator user creator so that's the first thing and the next thing i need is the user object as a part of the post body now that we can use just like we used to do for the previous version of dot net we can use from body and we can say user user and here we can say user creator dot create user and if the user is created then we will we want to return ok otherwise we want to return a bad request so how we are going to do it let me take it to the next line just for clarity and here what we can do is we can do just like we had okay and bad request here also we can do the same thing from a class called results so we can do results dot okay if the user creation was successful otherwise we can say results dot bad request and that's about it so let's run it and see it in action so for seeing it in action you know either have to use postman or i could have used sweater so let's go ahead and add swagger so for editing swagger of course we need to have the swatch buckle dot asp.net code so for that i can go ahead and install it in nuget package or i can just go ahead and edit in the property group if i do that also it will install it as a part of nuget package in the background so it would have done that already now what we have to do is we have to add the swagger related extension method to the dependency injection so for that i'll do builder dot services dot add add point api explorer which is needed and the second thing we have to do is builder dot services dot not add controller we need add swagger chain [Music] so once we add these to the next thing we have to do is add it to the middleware and for that we will do ab dot use swagger and f dot use swagger ui now sweater ui takes an action and we can do all the things that we usually do in dot number five i'm not going to do it here because it's not related to dot net 6 or cs10 if this is standard swagger so i'm just going to keep it at this and now i'm just going to run the application and once the application is started now if i go to swagger i can see the swagger api and i can see both post and get so i'm going to post the user going to try it out and i'm going to keep everything the same and if i execute now i can see it is a success and in the console i could see here user with name string is created as you can see here user with name string is created so the method was called and it successfully worked now one thing is that i am throwing a 400 but in swagger here i am not mentioning what i am throwing but what we can do is we can provide swagger documentation as a part of this request itself so what we can do here we can say first thing is we can specify a name so we can say with name and here we can give a name so this one is create user and then what we can do is we can say produces so what does it produces it produces user array and when it produces user array status status codes dot okay and also it produces status code status code dot pad request so now if i run this in the documentation i should see the 400 bad requests showing up which was not showing up earlier so now if i do sweater and if i expand you can see it is showing 200 when it shows 200 this is how the response is this is this is the schema so i give a user array which is wrong it does not produces user array so i can change that so it does not produce an user array it just gives the status so i don't need to keep it there i just have to say it either produces okay or 400 pad request let me run it again show you yeah as you can see it either gives you 200 or 400 bad requests so as you can see with only 15 lines of code and of course couple of other classes for creating and deleting user we just completed building a crowd operation for user now to be honest if we are using an entity framework or a dapper for create user and user provider the code is not going to be very different it's going to be similar it's going to be very three or four lines of code here for accessing db and three or four lines of code here for creating the data so technically between 30 to 50 lines of code will be done creating a micro service which allows you to create user and get user and i think it is extremely powerful this is very handy and very useful and i think it gives a very good chance for developers from other frameworks like node.js or python node.js with express and python with django to jump in c sharp and very quickly adapt it with this minimal api so that's all i wanted to cover for today's video if you like this video please give me a thumbs up if you are new to my channel and if you think you are getting value out of my channel please subscribe to my channel and thanks so much for watching this video
Info
Channel: DotNet Core Central
Views: 1,903
Rating: undefined out of 5
Keywords: minimal api .net 6, minimal api c# 10, minimal api .net 6 c# 10, .net 6 minimal api, c# 10 minimal api, c# 10 lambda improvements, lambda attributes c# 10, lambda explicit types c# 10, lambda explicit return types c# 10
Id: 695HrU8Z20c
Channel Id: undefined
Length: 24min 52sec (1492 seconds)
Published: Sun Nov 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.