Route Constraints: Validate the route variables | ASP.NET Core 5.0 Web API Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
while working with the routes in asp.net core web application either mvc application or the web api application then you need to implement some logics on these routes and all these logics are possible just because of the route constraints let's understand what is the concept of this route constraints let's say i want to implement a url where at the last part i am passing the id but this id must be less than 20. if the id is greater than or equal to 20 then we should not hit this particular resource second is the entire part of the url is same but in the last parameter here i want to pass the string in the first url i want to pass the indesign but here in the second one i want to pass the string then how can i differentiate in between these urls and you can notice that these are unique urls so these must be possible in web application now here is the third url for example in this domain i want to pass the id but the id must be greater than 20. we can handle all these type of scenarios by using the route constraint let's understand the route constraints we will start with first constraint type if we are using any kind of variable in a route then we can provide the type let's open the visual studio and let's understand how it works to understand the concept better let's add one more controller let's give the name books click on the add button here we can add one accent method so we can simply copy the action method from this place let's paste it over here so here we can define the routes like id let's get one more similar action method and this time here i will be using string [Music] let's update the message like hello end and here we can write hello string now let's focus on the routes in the routes first we are using api then the name of the controller because this is a base route and then after this base route we are appending this part so here we are passing the id and here again we are passing the id but here is a slight logic in the first action method we are expecting integer but here in the second one we are expecting a string let's see how it is working without doing anything else let's put a breakpoint on both the action methods and run this application in debug mode let's enter the url here we can write api books and then some id let's say first i'm typing integer click on the enter button and here you can notice that we are having the mbbt error here the application got confused because we have defined two resources for one single url and the application is right because we are using the same route for both the action methods now because i want to have the different type of this particular variable so we can define the type on these variables let's see how can we do that to assign the type of a particular variable first you need to use colon this colon is available on the same button of your semicolon and then you can provide the type let's say i want to have the type integer so here i can write integer by default all the variables will be traded as a string so there is no need to define anything like string over here now let's run this application again by pressing ctrl f5 we can reverse this page and this time you can see that we are having the output from integer hello integer 12. if instead of using this integer i am passing some text like this press the enter button here you can notice that we are having the message from our second accent method that is string this is how you can provide the type for your variables and remember you can use any type you can provide integer boolean datetime double float etc second constraint is minimum to set the minimum value we can use the main method and inside this main method we can provide the minimum number let's say i want to implement a logic on this action method that the minimum number of the id must be 10. so here i can add one more constraint again put a colon then type min and in this mean method we can provide the minimum value let's say 10. now this route will not work with less than 10 values let's run this application by pressing ctrl f5 again let's pass the id let's say i'm passing 12 press the enter button so we are getting the proper message like hello into 12 because 12 is greater than 10 that is why it is working but suppose instead of using 12 i am passing 1 press the enter button this time you can notice that we are navigated to the second one in the string as i have already told you that by default all the values will be treated as string and this is the reason that this time we are hitting the second action method next just like the minimum we can also define the maximum number to define the maximum number we need to use the max method and inside this max we can provide the maximum number for that particular variable suppose you are working with the id and you want to have the id that must be less than 100 so in the max method you can pass 100 and the route will work only for the less than 100 values let's try this one also let's say along with this minimum now i want to work with the max also so here i can define max and in the max method i can provide the maximum number let's say it is hundred save the changes build this application refresh this page here we are having a string for id1 let's define it like 99 press the enter button so we are having the message from integer but if i will update this value to greater than 100 just like 991 press the enter button and here we are having the string that is the second action method this is how you can define the constraints on your variable and you can protect your xm method suppose i am passing a string in the url and i want to define the minimum length of this string so i can use the main length for example we are talking about the name then in the name i want to define some minimum length i can use this mean length function and i can define this minimum length so let's say i want to define the mean length let's say the minimum length would be 5 now build this application refresh this page this time you can see because this id does not meet the requirement for any of the resource in the first one the id must be greater than 10 and less than 100 and in the second one there must be five characters in this value that is why we are having four zero four page if i will append the text like this time you can see we are having five characters press the enter button and here we go the message from the second accent method if i will update the value to 20 then the integer is working for us if again i am passing something abc this time i am passing a string but this string does not have five characters ideally we should get the four zero four piece and here it is we can also have the length more than 5 characters this time you can see the length of this particular variable is 9 that's the enter button and it is working because we have added the check only for the mill length not for the maximum length and this is the reason that this route is working fine second is max length just like the minimum length we can also apply the max length on this particular variable you have to try the max length by yourself next is length the length is basically to match the exact value suppose i am having length 15 if i'm using length 15 it means there must be exactly 15 characters in that particular string if there are less than 15 or greater than 15 then it will not work instead of using this min length this time i am using length and here i am passing 5 build this application refresh this page here you can notice that we are having 404 because we are passing 9 characters and this is not a valid path and there must be exactly five characters in this particular variable press the enter button here we got the message from our second action method that is hello string if you will pass less than 5 you will get the error if you will pass more than 5 then again you will get the error next is range although you can define a range by using the minimum and max method but you can also use the range method in the range method you can pass two numbers first is the minimum and second is the maximum so instead of using two separate functions here you can use only one function and you can define your range only by using the range method
Info
Channel: WebGentle
Views: 21,736
Rating: undefined out of 5
Keywords: asp.net core web api, web api asp.net core 5, web api asp.net core, .net 5 web api, api in .net core 5.0, asp.net core web api tutorial, asp.net web api core 5, Restful web api asp.net core, how to build restful apis with asp.net core 5, how to build restful apis with asp.net core, asp.net core, asp.net core web api tutorial for beginners, restful web api tutorial asp.net core, asp.net core web api tutorial webgentle, asp.net core web api tutorial nitish
Id: hUJEQqKNmio
Channel Id: undefined
Length: 8min 37sec (517 seconds)
Published: Fri Apr 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.