Repository navigation
ASPNET Core 5 MVC Bootstrap Responsive Web Programming Lecture 6 Routing Attribute Routing
ASP.NET Core 5 - MVC - Bootstrap - Responsive Web Programming, Lecture 6: Routing, Attribute Routing
Full tutorial link > https://www.youtube.com/watch?v=152Oyy96GpY
#SoftwareDevelopment #Software #Engineering #CSharp #Coding #Course
Asp.NET Core V5 - MVC Pattern - Bootstrap V5 - Responsive Web #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnAWtgk4ja3HN3xgMKF7BOE
GitHub repository of the course : https://github.com/FurkanGozukara/Responsive-Web-Design-With-ASP.NET-5-and-MVC-Pattern-and-BootStrap-5-2021
Discord channel link of the course : https://discord.gg/6Mrb8MwteQ
How to use Discord : https://youtu.be/AEwPtYiLvsQ
In lecture 6:
-
How to compose an empty from scratch ASP.NET Core MVC web application without any template
-
How to convert an empty ASP.NET web application into MVC application
-
How to support both custom routing and default Endpoints routing in an ASP.NET Core MVC application
-
What does URL routing mean in ASP.NET web application
-
The anatomy of an MVC Route
-
The Route template
-
Multiple routes to the same method of the same controller
-
What is Attribute routing
-
What is the difference between conventional routing and attribute routing
-
Advantageous of attribute routing
-
Routing Templates
-
Catch-all parameters
-
Optional parameters
-
Reserved routing names
-
Routing Constraints
-
Data type constraints
last page : https://asp.mvc-tutorial.com/layout/introduction/
Please subscribe and make comments.
This course requires fundamental knowledge about programming and C#. So please watch our below playlists related to C#.
Our courses playlists are like below:
[1] Introduction to Programming Full Course with C# playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffskoSXySh0MdiayPJsBZ7m2o
[2] Advanced #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslHaBdS3RUW26RKzSjkl8m4
[3] Object Oriented Programming Full Course with C# playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnH3XJb66FDIHh1yHwWC26I
[4] Asp.NET Core V5 - MVC Pattern - Bootstrap V5 - Responsive Web #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnAWtgk4ja3HN3xgMKF7BOE
[5] Artificial Intelligence (AI) and Machine Learning (ML) Full Course with C# Examples playlist: https://www.youtube.com/playlist?list=PL_pbwdIyffskVschrADCL6KEnL_nqDtgD
[6] Software Engineering Full Course playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslgxMVyXhnHiSn_EWTvx1G-
[7] Security of Information Systems Full Course playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslM_o92NwkaUzD7C6Fekx26
[8] (Turkish) Bilgisayar Becerileri Tam Ders playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsmyE2e909ea1MXLcMb8MenG
auto subtitle timing : NA
-
00:00:02 hello dear students welcome to the lecture six of response web design with
-
00:00:05 lecture six of response web design with asu.net python and mvc pattern and
-
00:00:07 asu.net python and mvc pattern and bootstrap five
-
00:00:09 bootstrap five uh so the today's topic is uh roading
-
00:00:12 uh so the today's topic is uh roading and roading is one of the most important
-
00:00:15 and roading is one of the most important and crucial
-
00:00:16 and crucial part of mvc uh
-
00:00:20 part of mvc uh pattern in esplanade that design
-
00:00:25 pattern in esplanade that design so also i am a bit sick today therefore
-
00:00:29 so also i am a bit sick today therefore i have a
-
00:00:30 i have a runny nose and so my voice may not be
-
00:00:33 runny nose and so my voice may not be very good
-
00:00:34 very good i am hoping that you will understand it
-
00:00:36 i am hoping that you will understand it and excuse me
-
00:00:38 and excuse me uh so for today's lecture i want to
-
00:00:42 uh so for today's lecture i want to start with an empty project and
-
00:00:45 start with an empty project and continue from that empty project
-
00:00:48 continue from that empty project so let's compose our new project
-
00:00:58 meanwhile we can read the introduction of loading
-
00:01:07 it'll often be a view okay it even starts from where i want it
-
00:01:15 routing introduction routing in mvc is the concept of mapping
-
00:01:17 routing in mvc is the concept of mapping a url
-
00:01:18 a url which is what the end user will be
-
00:01:20 which is what the end user will be requesting through their browser
-
00:01:22 requesting through their browser to a method in one of your controllers
-
00:01:24 to a method in one of your controllers which will then send a response back to
-
00:01:25 which will then send a response back to the browser
-
00:01:27 the browser the response will often be a view but it
-
00:01:29 the response will often be a view but it can also be a simple piece of text
-
00:01:31 can also be a simple piece of text a redirect or a number of other options
-
00:01:34 a redirect or a number of other options okay so you see that
-
00:01:36 okay so you see that [Music]
-
00:01:37 [Music] it is about a visitor a user
-
00:01:41 it is about a visitor a user to requesting a url and mapping that url
-
00:01:45 to requesting a url and mapping that url to a controller
-
00:01:47 to a controller so here this is the road url why
-
00:01:50 so here this is the road url why because it doesn't directly refer to a
-
00:01:52 because it doesn't directly refer to a page as you can see
-
00:01:54 page as you can see it has some let's say directorial
-
00:01:59 it has some let's say directorial setup such as a directory of routing and
-
00:02:01 setup such as a directory of routing and then introduction
-
00:02:03 then introduction so this is mapping to a page a
-
00:02:05 so this is mapping to a page a controller if this page
-
00:02:07 controller if this page is designed in s2.mvc
-
00:02:11 is designed in s2.mvc so this is what is about uh roading okay
-
00:02:14 so this is what is about uh roading okay and if our project ready so from here
-
00:02:18 and if our project ready so from here we are going to so.net core empty
-
00:02:23 we are going to so.net core empty or
-
00:02:40 okay here is that net core web up model weave controller here this is what we
-
00:02:42 weave controller here this is what we need
-
00:02:53 okay let's next and let's pick our directory
-
00:03:03 okay lecture six next
-
00:03:15 options default routing if you're working on a brand new project
-
00:03:17 if you're working on a brand new project created from the empty template in
-
00:03:19 created from the empty template in visual studio
-
00:03:20 visual studio then your application doesn't have any
-
00:03:22 then your application doesn't have any routing yet
-
00:03:23 routing yet we talked a bit about routing in the
-
00:03:25 we talked a bit about routing in the article about adding a controller to the
-
00:03:27 article about adding a controller to the project
-
00:03:27 project but if you haven't followed this
-
00:03:29 but if you haven't followed this tutorial from the start we need to check
-
00:03:31 tutorial from the start we need to check up on this
-
00:03:32 up on this okay okay so we are selecting.net core
-
00:03:37 okay okay so we are selecting.net core five
-
00:03:38 five authentication none configure for https
-
00:03:41 authentication none configure for https okay i will
-
00:03:45 [Music] leave options as default
-
00:03:53 so you see it says it can be run on linux macos windows cloud service web
-
00:03:56 linux macos windows cloud service web in c sharp okay
-
00:04:00 in c sharp okay so let's continue this
-
00:04:04 so let's continue this try opening your startup.cs file which
-
00:04:07 try opening your startup.cs file which should have been added to your project
-
00:04:08 should have been added to your project when you created it if there's nothing
-
00:04:10 when you created it if there's nothing about routing
-
00:04:10 about routing you need to add it manually okay okay
-
00:04:14 you need to add it manually okay okay one moment
-
00:04:15 one moment okay
-
00:04:22 okay so our project has started but you see it is not empty
-
00:04:25 see it is not empty because we have selected an existing
-
00:04:27 because we have selected an existing project
-
00:04:29 project okay and yeah i will
-
00:04:33 okay and yeah i will i will compose an empty project instead
-
00:04:35 i will compose an empty project instead of this so
-
00:04:42 but we can continue with this as well but let's see
-
00:04:49 okay i will i will composing an empty project so
-
00:04:50 project so it will be better for us to understand
-
00:04:56 it will be better for us to understand okay let's delete the existing project
-
00:05:10 and let's open you see you can also use a project
-
00:05:13 you see you can also use a project template that is
-
00:05:14 template that is generated by by the asp.net
-
00:05:18 generated by by the asp.net it includes many of the elements by
-
00:05:20 it includes many of the elements by default
-
00:05:22 default however for learning better
-
00:05:25 however for learning better i will compose an an empty project
-
00:05:28 i will compose an an empty project instead of a
-
00:05:30 instead of a ready template so let's reopen our
-
00:05:34 ready template so let's reopen our visual studio
-
00:05:38 visual studio create new project
-
00:05:50 it is looking for this in project templates we can continue
-
00:05:59 manually the easiest way is to use the two helper methods called use routing
-
00:06:01 two helper methods called use routing and map default controller route it adds
-
00:06:03 and map default controller route it adds a default fallback
-
00:06:05 a default fallback route to your project which will try to
-
00:06:07 route to your project which will try to map urls to methods on one of your
-
00:06:09 map urls to methods on one of your controllers
-
00:06:10 controllers if no controller name is specified the
-
00:06:13 if no controller name is specified the url will be mapped to the home
-
00:06:14 url will be mapped to the home controller
-
00:06:15 controller okay so this is about default routing
-
00:06:18 okay so this is about default routing which we were using
-
00:06:20 which we were using okay let's so this is not netweb core
-
00:06:24 okay let's so this is not netweb core model view controllers
-
00:06:30 but it is a template what we need is we need empty project
-
00:07:08 okay i think we need a sp.net course not uh
-
00:07:09 uh yes we need asp.net core empty
-
00:07:17 yes we need asp.net core empty yeah so this will be the thing that you
-
00:07:20 yeah so this will be the thing that you are going to use
-
00:07:22 are going to use it will be an empty project so we will
-
00:07:24 it will be an empty project so we will generate
-
00:07:26 generate everything ourselves
-
00:07:31 everything ourselves okay
-
00:07:55 okay so it says that inside startup cs we are going to have a configuration
-
00:07:57 we are going to have a configuration like this
-
00:08:02 okay so yes this is an empty project currently this does not support mvc
-
00:08:05 currently this does not support mvc pattern
-
00:08:06 pattern or anything as you can see
-
00:08:13 so we are going to enable the mvc out of it however how we were doing it let's open
-
00:08:17 however how we were doing it let's open our
-
00:08:18 our lecture 5 code and check for it
-
00:08:26 okay and here's we have program cs and startups yes let's open the program cs
-
00:08:29 startups yes let's open the program cs with notepad plus plus
-
00:08:38 okay so we have create host builders inside program cs yeah it's already
-
00:08:42 inside program cs yeah it's already here okay in programs yes we don't
-
00:08:45 here okay in programs yes we don't change anything
-
00:08:46 change anything so let's check for startup cs
-
00:08:50 so let's check for startup cs okay we add services at mvc this will
-
00:08:53 okay we add services at mvc this will turn our.net core application into an
-
00:08:57 our.net core application into an mvis application it is added inside
-
00:09:01 mvis application it is added inside configure services
-
00:09:04 configure services and yeah the rest is uh
-
00:09:08 and yeah the rest is uh same so we can continue our tutorial
-
00:09:12 same so we can continue our tutorial okay so it tells us to add this and
-
00:09:15 okay so it tells us to add this and let's see what does that do
-
00:09:24 okay so it says that off use rhodic it says add the microsoft excel.net core
-
00:09:26 it says add the microsoft excel.net core loading and points rolling middle waves
-
00:09:28 loading and points rolling middle waves to the specified application will dis
-
00:09:32 to the specified application will dis okay so it will be it will enable our
-
00:09:35 okay so it will be it will enable our routing
-
00:09:36 routing and as an end point we map the default
-
00:09:39 and as an end point we map the default controller so when i
-
00:09:40 controller so when i run my application right now what would
-
00:09:43 run my application right now what would happen
-
00:09:45 happen nothing would be displayed because we
-
00:09:46 nothing would be displayed because we don't have the default wave or
-
00:09:49 don't have the default wave or controller
-
00:09:51 controller okay so we are going to add them as well
-
00:09:53 okay so we are going to add them as well and how we were adding them we were adding
-
00:09:56 how we were adding them we were adding controllers
-
00:09:57 controllers and weaves right
-
00:10:00 and weaves right okay so i will also add them but
-
00:10:03 okay so i will also add them but i want to show you what happens when we
-
00:10:05 i want to show you what happens when we run this application
-
00:10:20 you see nothing happens okay so let's add our
-
00:10:21 add our mvc pattern having folders
-
00:10:25 mvc pattern having folders so first i will add controllers
-
00:10:29 so first i will add controllers you see you have to use exact naming as
-
00:10:31 you see you have to use exact naming as controllers and inside controller i am
-
00:10:33 controllers and inside controller i am going to add home controller so
-
00:10:35 going to add home controller so it will be the default controller for
-
00:10:45 home page of our weave okay i'm adding add i'm missing
-
00:10:47 okay i'm adding add i'm missing controller
-
00:10:50 controller you see it is by default so default
-
00:10:52 you see it is by default so default routing goes to home controller and i
-
00:10:55 routing goes to home controller and i also need
-
00:10:55 also need a v because it is returning a vm however
-
00:10:58 a v because it is returning a vm however this is not necessary
-
00:10:59 this is not necessary because i can return a message as well
-
00:11:04 because i can return a message as well so it was return
-
00:11:08 so it was return content or something yeah it returned
-
00:11:11 content or something yeah it returned content
-
00:11:13 content test so i will just comment this out
-
00:11:17 test so i will just comment this out now our application should work because
-
00:11:20 now our application should work because the default controller will map to
-
00:11:23 the default controller will map to home index action okay
-
00:11:26 home index action okay i will show you in a minute let's run
-
00:11:28 i will show you in a minute let's run the application
-
00:11:36 and i will explain what i mean okay
-
00:11:44 since we have set default controller in our startup cs here
-
00:11:46 our startup cs here uh it will map to default controller
-
00:11:48 uh it will map to default controller root
-
00:11:49 root okay you see test is running this is
-
00:11:52 okay you see test is running this is equal to
-
00:11:53 equal to home dot index action okay
-
00:11:56 home dot index action okay you see save so this is the default
-
00:12:00 you see save so this is the default controller rooting home.index
-
00:12:03 controller rooting home.index okay and then we can add our give as
-
00:12:07 okay and then we can add our give as well
-
00:12:08 well our default view so first we need to
-
00:12:11 our default view so first we need to compose another
-
00:12:13 compose another folder as views okay and here
-
00:12:17 folder as views okay and here i am going to add another folder as uh
-
00:12:20 i am going to add another folder as uh home if i remember correctly
-
00:12:24 home if i remember correctly yes and under this one i am going to add
-
00:12:28 yes and under this one i am going to add index because by default we are calling
-
00:12:31 index because by default we are calling index view in the previous lectures we
-
00:12:34 index view in the previous lectures we have
-
00:12:35 have shown how to define different indexes
-
00:12:39 shown how to define different indexes different names
-
00:12:41 different names and everything but just
-
00:12:44 and everything but just stick to the tutorial for now
-
00:12:49 stick to the tutorial for now and index cshtml so when you
-
00:12:52 and index cshtml so when you call return view it will look for by
-
00:12:56 call return view it will look for by default
-
00:12:57 default since we use default loading
-
00:13:00 since we use default loading it will look for
-
00:13:04 it will look for years and home
-
00:13:07 years and home and index cshtml okay
-
00:13:11 and index cshtml okay so this is equal to this one
-
00:13:15 so this is equal to this one let me show you
-
00:13:23 okay let's try both of them so we use okay home
-
00:13:31 index series html and let's say
-
00:13:40 whatever the message you want to show so let's run the application to see
-
00:13:43 let's run the application to see okay so here we are providing full patch
-
00:13:46 okay so here we are providing full patch and here this is the default
-
00:13:48 and here this is the default currently this will not run because we
-
00:13:50 currently this will not run because we are returning here
-
00:13:51 are returning here but i just want to show you how it works
-
00:14:01 this returning view is not related to loading we can return anything here
-
00:14:03 loading we can return anything here but uh home controller and index
-
00:14:07 but uh home controller and index action is about default okay it is
-
00:14:09 action is about default okay it is working as expected
-
00:14:12 working as expected and we can also run it like this
-
00:14:44 okay so let's continue one moment
-
00:14:52 okay and from here i will continue reading
-
00:14:59 thanks to these methods your application now supports basic routing
-
00:15:01 now supports basic routing here are some examples of urls that now
-
00:15:04 here are some examples of urls that now works and the controller slash method
-
00:15:06 works and the controller slash method combination that they map to
-
00:15:08 combination that they map to okay so it works with a home controller
-
00:15:11 okay so it works with a home controller index home
-
00:15:12 index home index and by there sorry
-
00:15:16 index and by there sorry and the local host works home index
-
00:15:18 and the local host works home index works
-
00:15:19 works tests will work only if you have a test
-
00:15:23 tests will work only if you have a test action in home controllers and home
-
00:15:25 action in home controllers and home something will work
-
00:15:27 something will work so let's also make them okay
-
00:15:30 so let's also make them okay we have seen that but i will uh show
-
00:15:32 we have seen that but i will uh show again
-
00:15:34 again so this time i will make a test and it
-
00:15:36 so this time i will make a test and it will return
-
00:15:37 will return test like this and then
-
00:15:47 i will make the other one as well something okay so the something will
-
00:15:51 something okay so the something will return something
-
00:15:52 return something okay okay let's run and check
-
00:15:56 okay okay let's run and check each default voting mechanisms
-
00:16:00 each default voting mechanisms to understand
-
00:16:07 routing is really crucial for you to understand
-
00:16:08 understand it is really important because
-
00:16:11 it is really important because how you define your website navigation
-
00:16:15 how you define your website navigation totally depends on your
-
00:16:16 totally depends on your roading okay so this is required
-
00:16:21 roading okay so this is required from that index so now i can call
-
00:16:25 from that index so now i can call on the test it is working i can call
-
00:16:28 on the test it is working i can call honda something working
-
00:16:30 honda something working can i call like this no
-
00:16:33 can i call like this no it is not working why because
-
00:16:37 it is not working why because when you type something like that it is
-
00:16:39 when you type something like that it is expecting another input
-
00:16:41 expecting another input it is not available
-
00:16:48 however let's see if this will work no
-
00:16:50 no here it says it will work but it is not
-
00:16:52 here it says it will work but it is not working
-
00:17:02 yeah this is this information is not valid anymore you have
-
00:17:03 valid anymore you have to type home okay
-
00:17:09 to type home okay okay and so this is working
-
00:17:13 okay and so this is working this is working this is working but this
-
00:17:15 this is working this is working but this is not working anymore
-
00:17:16 is not working anymore in the recent version
-
00:17:20 in the recent version okay let's continue our meeting here
-
00:17:26 but it doesn't just affect the home controller instead it adds general
-
00:17:29 controller instead it adds general support to access a method on a
-
00:17:30 support to access a method on a controller without having to create
-
00:17:32 controller without having to create specific routes for it
-
00:17:34 specific routes for it so if you have a controller called user
-
00:17:36 so if you have a controller called user controller which has a method called
-
00:17:38 controller which has a method called details
-
00:17:39 details you could access it by calling the
-
00:17:40 you could access it by calling the following url slash user slash details
-
00:17:43 following url slash user slash details okay let's just compose a user details
-
00:17:47 okay let's just compose a user details immediately and see to do that
-
00:17:51 immediately and see to do that we are going to add a controller to our
-
00:17:54 we are going to add a controller to our controller folder
-
00:18:01 okay so it will be users controllers you see you have to add
-
00:18:02 see you have to add this to end of it so that the asp.net
-
00:18:05 this to end of it so that the asp.net can understand that you are calling a
-
00:18:08 understand that you are calling a controller
-
00:18:15 and inside viv uh i can add user controller how am i going to do
-
00:18:17 user controller how am i going to do that i am going to add
-
00:18:18 that i am going to add a folder as user
-
00:18:22 a folder as user and here i will add as in next but i can
-
00:18:25 and here i will add as in next but i can add another thing as well i will add
-
00:18:28 add another thing as well i will add details this time
-
00:18:39 and details page of user controllers
-
00:18:43 page of user controllers so in user controller i have to define
-
00:18:46 so in user controller i have to define these as details to call details
-
00:18:50 as details to call details or alternatively i can do like something
-
00:18:53 or alternatively i can do like something like this
-
00:18:55 like this index and
-
00:18:59 index and return vf details okay
-
00:19:02 return vf details okay so both should work let's test it
-
00:19:15 these are all features of default routing
-
00:19:16 routing in asp.net mvc
-
00:19:25 okay to access user home controller i will have user slash
-
00:19:32 okay user dot index okay it is working user dot
-
00:19:35 index okay it is working user dot details it's working as well so you see
-
00:19:39 details it's working as well so you see i can
-
00:19:40 i can call access it like this and this so
-
00:19:42 call access it like this and this so index
-
00:19:43 index is the action part here if i type index
-
00:19:47 is the action part here if i type index it will go into the index section if i
-
00:19:49 it will go into the index section if i type details it will go into the details
-
00:19:51 type details it will go into the details action okay
-
00:19:53 action okay but in each case it will go to the user
-
00:19:56 but in each case it will go to the user controller because it is taking the
-
00:19:58 controller because it is taking the controller name
-
00:19:59 controller name here by default routing
-
00:20:03 here by default routing this is possible because we have added
-
00:20:06 this is possible because we have added default routing otherwise it wouldn't be
-
00:20:08 default routing otherwise it wouldn't be possible
-
00:20:09 possible okay
-
00:20:18 details in other words the default route acts as a sort of catch-all rule
-
00:20:19 acts as a sort of catch-all rule to make it easy for you to get some
-
00:20:21 to make it easy for you to get some basic pages up and running
-
00:20:23 basic pages up and running but of course this only works for you if
-
00:20:26 but of course this only works for you if all your content follows the same
-
00:20:27 all your content follows the same pattern slash
-
00:20:28 pattern slash name of controller slash name of method
-
00:20:31 name of controller slash name of method so you see default routing
-
00:20:33 so you see default routing acts such as catch all rule therefore if
-
00:20:36 acts such as catch all rule therefore if we follow name of controller slash name
-
00:20:39 we follow name of controller slash name of method it will work
-
00:20:41 of method it will work here we are following that name of
-
00:20:43 here we are following that name of controller username of method
-
00:20:45 controller username of method action details we can see it the name of
-
00:20:48 action details we can see it the name of method
-
00:20:49 method is index or details that is how it works
-
00:20:53 is index or details that is how it works that is how application understands it
-
00:20:55 that is how application understands it because we are following the
-
00:20:57 because we are following the pattern okay
-
00:21:00 pattern okay name of method as soon as you deviate
-
00:21:04 name of method as soon as you deviate from this
-
00:21:04 from this eg because you don't want the specific
-
00:21:06 eg because you don't want the specific name of your controller to appear in the
-
00:21:08 name of your controller to appear in the url
-
00:21:09 url you will need to add your own rules
-
00:21:12 you will need to add your own rules don't worry you can still rely on the
-
00:21:13 don't worry you can still rely on the default routing
-
00:21:14 default routing simply by adding your own rules before
-
00:21:16 simply by adding your own rules before the default routing is applied
-
00:21:19 the default routing is applied more on that later okay so we can also
-
00:21:22 more on that later okay so we can also add our rules
-
00:21:23 add our rules uh our routing rules and we can still
-
00:21:26 uh our routing rules and we can still depend on the default running so
-
00:21:28 depend on the default running so how do we do that we add our own rules
-
00:21:31 how do we do that we add our own rules before the default reading is applied we
-
00:21:34 before the default reading is applied we will see that
-
00:21:35 will see that in the following pages
-
00:21:40 in the following pages simply by adding your own rules before
-
00:21:42 simply by adding your own rules before the default routing is applied
-
00:21:45 the default routing is applied more on that later summary
-
00:21:49 more on that later summary routing is the process of mapping urls
-
00:21:52 routing is the process of mapping urls which is how a user accesses a specific
-
00:21:54 which is how a user accesses a specific page or functionality in your
-
00:21:56 page or functionality in your application
-
00:21:57 application to the controller and the method on that
-
00:21:58 to the controller and the method on that controller which should be responsible
-
00:22:00 controller which should be responsible for creating the response to the url
-
00:22:03 for creating the response to the url thanks to default routing as we have
-
00:22:05 thanks to default routing as we have seen in this article
-
00:22:06 seen in this article you can get the most basic routing
-
00:22:08 you can get the most basic routing functionality very easy
-
00:22:09 functionality very easy but for more complex situations you will
-
00:22:12 but for more complex situations you will need to create your own roots
-
00:22:14 need to create your own roots no worries we will talk much more about
-
00:22:16 no worries we will talk much more about just that in the next articles
-
00:22:19 just that in the next articles okay so let's continue
-
00:22:24 okay the anatomy off and we see root let's start from here
-
00:22:32 routing the anatomy of an nvc route in this tutorial we have already
-
00:22:35 route in this tutorial we have already talked about how you can easily get up
-
00:22:37 talked about how you can easily get up and running with basic routing support
-
00:22:39 and running with basic routing support thanks to default
-
00:22:40 thanks to default routing however it's a great idea to
-
00:22:43 routing however it's a great idea to understand just how roots are defined
-
00:22:45 understand just how roots are defined because at one point you will likely
-
00:22:47 because at one point you will likely need to step outside the comfort of the
-
00:22:49 need to step outside the comfort of the default slash catch all route and create
-
00:22:51 default slash catch all route and create your own
-
00:22:52 your own with that in mind let's talk a bit about
-
00:22:54 with that in mind let's talk a bit about how a root is defined and what it looks
-
00:22:56 how a root is defined and what it looks like
-
00:22:58 like the most important part of a routing
-
00:22:59 the most important part of a routing rule is to translate a url into a
-
00:23:02 rule is to translate a url into a controller method
-
00:23:03 controller method okay so the most important part is
-
00:23:06 okay so the most important part is translating url into
-
00:23:08 translating url into into your controller so when we type
-
00:23:10 into your controller so when we type this url
-
00:23:11 this url it has to be translated into a
-
00:23:13 it has to be translated into a controller method
-
00:23:15 controller method so that the uh s dot engine can return
-
00:23:19 so that the uh s dot engine can return you the proper content okay so it is about
-
00:23:23 the proper content okay so it is about uh translation of addresses to the
-
00:23:26 uh translation of addresses to the methods
-
00:23:27 methods okay method
-
00:23:31 okay method for that we need the url path and the
-
00:23:33 for that we need the url path and the name of the controller and the method
-
00:23:36 name of the controller and the method the system needs to be able to identify
-
00:23:38 the system needs to be able to identify the rule and you might also like a
-
00:23:39 the rule and you might also like a reminder about the purpose of the rule
-
00:23:41 reminder about the purpose of the rule if you have a lot of them so we also
-
00:23:43 if you have a lot of them so we also need to give the rule a name
-
00:23:45 need to give the rule a name okay so what we need is uh we need to
-
00:23:48 okay so what we need is uh we need to compose a rule
-
00:23:50 compose a rule that will also have a name to identify
-
00:23:52 that will also have a name to identify the rule
-
00:23:53 the rule later and that rule will translate
-
00:23:57 later and that rule will translate the url into and
-
00:24:00 the url into and controller and the method of that
-
00:24:03 controller and the method of that controller okay
-
00:24:10 name with that in mind let's try creating a very basic rule
-
00:24:12 creating a very basic rule as mentioned rules can be defined in
-
00:24:14 as mentioned rules can be defined in several ways and places
-
00:24:16 several ways and places but we'll start out doing it in the
-
00:24:17 but we'll start out doing it in the startup.cs file
-
00:24:19 startup.cs file inside the configure method okay so this
-
00:24:22 inside the configure method okay so this is how we can do it inside startup cs
-
00:24:24 is how we can do it inside startup cs configure method
-
00:24:26 configure method and so let's copy and paste it
-
00:24:29 and so let's copy and paste it okay startup is here and configure is
-
00:24:33 okay startup is here and configure is here
-
00:24:35 here okay it says your routing and here
-
00:24:39 okay it says your routing and here use and we see rules okay so it says
-
00:24:44 use and we see rules okay so it says that
-
00:25:01 okay so if we use endpoints then we can't use
-
00:25:02 can't use mvc
-
00:25:10 therefore how can we combine them
-
00:25:28 okay let's remove this and see what happens
-
00:25:44 oh i have to remove these as well about probably
-
00:25:57 okay and so okay so what it says
-
00:26:09 inside configure services okay so what we need is we need to set
-
00:26:15 so what we need is we need to set it is say telling us and we see options
-
00:26:18 it is say telling us and we see options enabled to end point rooting
-
00:26:21 enabled to end point rooting and we see
-
00:26:47 and we see options to continue using a user miss preset and we see options
-
00:26:50 user miss preset and we see options and even
-
00:27:05 let's look for this error message and see what it says
-
00:27:19 okay you see after i have clicked the error message it has
-
00:27:21 error message it has opened the page and i'm going to edit
-
00:27:28 oh i see so instead of this i will use this and let's see how will it work
-
00:27:37 so i will add this to my before my default routing and here
-
00:27:41 before my default routing and here i will add um a custom route
-
00:27:45 i will add um a custom route such as custom rod
-
00:27:49 such as custom rod name so it will take
-
00:27:53 name so it will take product list and the controller will be
-
00:27:57 product list and the controller will be home instead of product
-
00:28:00 home instead of product and action will be index okay so let's
-
00:28:03 and action will be index okay so let's see what happens
-
00:28:04 see what happens if it works or not
-
00:28:10 you see this is the pattern we have defined it
-
00:28:11 defined it however it will call home controller
-
00:28:14 however it will call home controller with
-
00:28:15 with index action instead of products
-
00:28:18 index action instead of products controller with list action
-
00:28:20 controller with list action okay i wonder if this would work or not
-
00:28:24 okay i wonder if this would work or not okay default routing looks like working
-
00:28:28 okay default routing looks like working let's test it from index working
-
00:28:32 let's test it from index working what else we have we have users users
-
00:28:35 what else we have we have users users index
-
00:28:37 index working details working uh
-
00:28:40 working details working uh let's say yes and now time to check our
-
00:28:43 let's say yes and now time to check our custom route
-
00:28:45 custom route which takes products and then
-
00:28:48 which takes products and then lists yes it is also working
-
00:28:52 lists yes it is also working so you see this is how we add a custom
-
00:28:55 so you see this is how we add a custom route as we want
-
00:28:57 route as we want i can make this
-
00:28:59 i can make this [Music]
-
00:29:00 [Music] whatever as i whatever as i want for
-
00:29:03 whatever as i whatever as i want for example if i make it like this let's see
-
00:29:06 example if i make it like this let's see just products
-
00:29:11 i don't want to put anything to end of the end of it or
-
00:29:14 the end of it or any other extension
-
00:29:27 okay products yes it is also working nice you see we can define it
-
00:29:30 nice you see we can define it as we want i can also make it as
-
00:29:33 as we want i can also make it as something home or let's say index
-
00:29:37 something home or let's say index okay just index and then i run it
-
00:29:40 okay just index and then i run it we will see
-
00:29:55 oh one more unit but maybe we can define them
-
00:29:57 them in single
-
00:30:00 in single row like this
-
00:30:22 okay so let's make it properly
-
00:31:00 or maybe like this i am trying to add multiple
-
00:31:01 multiple options to the root let's try like this
-
00:31:46 moment i am trying to change so that it can
-
00:31:49 i am trying to change so that it can work
-
00:31:56 yeah let's try like this okay so i have chained it
-
00:31:57 chained it map roots map root like this i don't
-
00:32:01 map roots map root like this i don't know it is the proper way but
-
00:32:04 know it is the proper way but we can find it later
-
00:32:14 we can find it later okay something has given enough it says
-
00:32:17 okay something has given enough it says an item with the same k um
-
00:32:19 an item with the same k um [Music]
-
00:32:20 [Music] yeah because we have used this one
-
00:32:28 therefore we have to give another name okay let's
-
00:32:30 we have to give another name okay let's say
-
00:32:32 say index root now we should work
-
00:32:36 index root now we should work you see it depends on the keys that you
-
00:32:38 you see it depends on the keys that you add for
-
00:32:39 add for the name of the root is really important
-
00:32:47 so that it can keep track of that i i think we can also each time
-
00:32:49 think we can also each time define up user missy
-
00:33:02 okay it's working so let's try index yes it is working so this is working
-
00:33:05 yes it is working so this is working however it doesn't look very well
-
00:33:07 however it doesn't look very well uh so maybe we can define it
-
00:33:13 uh so maybe we can define it for each one like this as well i'm not
-
00:33:15 for each one like this as well i'm not sure
-
00:33:17 sure so let's compose another one
-
00:33:31 okay you see this works and this is also working
-
00:33:38 and this is also working okay
-
00:34:01 yes each one is working let's continue with the tutorial
-
00:34:08 all right so with this approach we are able to define our custom routings as
-
00:34:10 able to define our custom routings as well
-
00:34:11 well as well as keeping our default routing
-
00:34:13 as well as keeping our default routing as well
-
00:34:21 and where are we now okay we are here for reading the call to maproot
-
00:34:24 for reading the call to maproot takes several parameters in this case we
-
00:34:26 takes several parameters in this case we supply a name for the root
-
00:34:28 supply a name for the root the first parameter the url template for
-
00:34:30 the first parameter the url template for the rule the second parameter
-
00:34:32 the rule the second parameter as well as an object of defaults the
-
00:34:34 as well as an object of defaults the third parameter which will direct the
-
00:34:36 third parameter which will direct the rule to the desired controller and
-
00:34:38 rule to the desired controller and method on that controller
-
00:34:40 method on that controller so with this in place we can now call
-
00:34:42 so with this in place we can now call the url http colon slash slash localhost
-
00:34:46 the url http colon slash slash localhost slash products slash list
-
00:34:47 slash products slash list and the list method on the products
-
00:34:49 and the list method on the products controller will be called
-
00:34:52 controller will be called the root template in the above example
-
00:34:55 the root template in the above example the most interesting parameter is the
-
00:34:57 the most interesting parameter is the second one
-
00:34:59 second one it defines the template of the root and
-
00:35:01 it defines the template of the root and while our template was very simple
-
00:35:03 while our template was very simple there are a lot more possibilities
-
00:35:04 there are a lot more possibilities allowing you to support very
-
00:35:06 allowing you to support very sophisticated and complex urls
-
00:35:09 sophisticated and complex urls first of all you can define the default
-
00:35:11 first of all you can define the default controller and method slash action
-
00:35:13 controller and method slash action directly in the template like this
-
00:35:16 directly in the template like this roots dot maproot default controller
-
00:35:18 roots dot maproot default controller equals home
-
00:35:19 equals home slash action equals index
-
00:35:23 slash action equals index this is generally referred to as
-
00:35:24 this is generally referred to as conventional routing because it consists
-
00:35:26 conventional routing because it consists of at least two parts
-
00:35:28 of at least two parts where the first part refers to the
-
00:35:29 where the first part refers to the controller method and the second part
-
00:35:31 controller method and the second part refers to the action slash method
-
00:35:34 refers to the action slash method the third and optional part is usually
-
00:35:36 the third and optional part is usually an id allowing you to refer to a
-
00:35:38 an id allowing you to refer to a specific entity you would like to
-
00:35:40 specific entity you would like to display
-
00:35:40 display eg a specific product instead of a list
-
00:35:43 eg a specific product instead of a list of products and
-
00:35:43 of products and so on you can define this optional id
-
00:35:47 so on you can define this optional id parameter in the root as well
-
00:35:48 parameter in the root as well using a special syntax let's try a more
-
00:35:52 using a special syntax let's try a more complex example of a route
-
00:35:53 complex example of a route where we use several of the techniques
-
00:35:55 where we use several of the techniques just described
-
00:35:57 just described okay so we can also add an optional id
-
00:36:00 okay so we can also add an optional id and how it is optional it is optional
-
00:36:03 and how it is optional it is optional because you see there is a question mark
-
00:36:05 because you see there is a question mark at the end
-
00:36:07 at the end so let's define this um
-
00:36:10 so let's define this um such as like this okay so when we get
-
00:36:14 such as like this okay so when we get defined it like this it will pass also
-
00:36:16 defined it like this it will pass also id parameter to the
-
00:36:18 id parameter to the home action index
-
00:36:26 here so let's add another method
-
00:36:36 which will return something like this okay it will be
-
00:36:38 okay it will be integer id
-
00:36:41 integer id and pass its content
-
00:36:52 id okay and if let's name it as
-
00:37:08 yes it will just call this action okay
-
00:37:22 so the first one is key the other one is is the
-
00:37:23 is the url scientix and the
-
00:37:27 url scientix and the parse here is defining the which
-
00:37:29 parse here is defining the which controller and the action it will take
-
00:37:37 okay so when we provide no id let's see what happens
-
00:37:40 no id let's see what happens okay pass it id is zero and then we
-
00:37:43 okay pass it id is zero and then we define like this yes it is working you
-
00:37:45 define like this yes it is working you see
-
00:37:46 see i have added id to the end of the url
-
00:37:49 i have added id to the end of the url and now it works i could also do it
-
00:37:51 and now it works i could also do it something like this i believe
-
00:37:53 something like this i believe yes you see this is not
-
00:37:57 yes you see this is not sao
-
00:38:04 type of url so if you want to apply sao techniques to your website
-
00:38:08 sao techniques to your website you can make it something like this so
-
00:38:10 you can make it something like this so this looks much better than this
-
00:38:12 this looks much better than this as you can also as you would also agree
-
00:38:21 okay with this way you can uh get in exit each one of your products
-
00:38:24 get in exit each one of your products with different pages however if you apply
-
00:38:27 different pages however if you apply this
-
00:38:28 this they may not they may not get
-
00:38:31 they may not they may not get all indexes probably they would what but
-
00:38:34 all indexes probably they would what but this is not
-
00:38:37 this is not sao friendly okay
-
00:38:46 sao friendly okay and this is
-
00:38:53 this is sao franklin as you can see this is the difference
-
00:38:55 is the difference all right so let's continue
-
00:39:00 all right so let's continue one moment
-
00:39:13 the template now matches urls which starts with the word products
-
00:39:16 starts with the word products if nothing else is specified it will
-
00:39:18 if nothing else is specified it will default to the index
-
00:39:19 default to the index action slash method on the other hand
-
00:39:22 action slash method on the other hand if an action is specified in the second
-
00:39:24 if an action is specified in the second part of the url
-
00:39:25 part of the url it will be used at the end we have an
-
00:39:28 it will be used at the end we have an optional id parameter the fact that it's
-
00:39:30 optional id parameter the fact that it's optional is noted with the use of a
-
00:39:32 optional is noted with the use of a question mark
-
00:39:34 question mark we now support the following urls
-
00:39:37 we now support the following urls slash products slash slash
-
00:39:41 slash products slash slash products slash list slash
-
00:39:44 products slash list slash slash products slash details slash 32
-
00:39:47 slash products slash details slash 32 slab
-
00:39:48 slab okay so here using dts method there is
-
00:39:51 okay so here using dts method there is list method and the default in next method
-
00:39:55 method and the default in next method so each one of them is being supported
-
00:40:00 so each one of them is being supported okay in our case
-
00:40:11 okay we we also support yeah damn
-
00:40:20 notice especially the details methods it has a parameter called id
-
00:40:22 has a parameter called id which matches the parameter we specified
-
00:40:24 which matches the parameter we specified in the root
-
00:40:25 in the root this means that if you call the details
-
00:40:27 this means that if you call the details url the id you append to it will
-
00:40:30 url the id you append to it will automatically be passed as the id
-
00:40:32 automatically be passed as the id parameter to the details method
-
00:40:35 parameter to the details method as you can see the routing template
-
00:40:37 as you can see the routing template system is very flexible
-
00:40:38 system is very flexible and allows you to support many different
-
00:40:40 and allows you to support many different url variations
-
00:40:42 url variations there are many more possibilities which
-
00:40:44 there are many more possibilities which we'll look into later in this tutorial
-
00:40:46 we'll look into later in this tutorial but hopefully these examples have given
-
00:40:48 but hopefully these examples have given you a good idea on just how powerful the
-
00:40:50 you a good idea on just how powerful the routing mechanisms in asp.net core mvc
-
00:40:53 routing mechanisms in asp.net core mvc is
-
00:40:59 okay so we can also type the action name like here instead
-
00:41:02 type the action name like here instead of writing here both is passable
-
00:41:06 writing here both is passable you see we also define action here or we
-
00:41:08 you see we also define action here or we can define the action here
-
00:41:10 can define the action here as well okay
-
00:41:18 and let's continue cts
-
00:41:28 multiple routes as previously mentioned you can of course have more than one
-
00:41:30 you can of course have more than one route in your application
-
00:41:32 route in your application in fact the most common approach is to
-
00:41:34 in fact the most common approach is to have a default slash catch-all route
-
00:41:36 have a default slash catch-all route as we saw in the previous article but
-
00:41:38 as we saw in the previous article but keep it in the bottom of the list of
-
00:41:40 keep it in the bottom of the list of routes
-
00:41:41 routes why because the routing rules are
-
00:41:44 why because the routing rules are processed from the top and down
-
00:41:45 processed from the top and down until a match is found when a match
-
00:41:47 until a match is found when a match occurs no more roots are processed
-
00:41:50 occurs no more roots are processed in other words you should have your most
-
00:41:52 in other words you should have your most specific rules at the top
-
00:41:54 specific rules at the top and then keep the less specific slash
-
00:41:56 and then keep the less specific slash catch-all roots at the bottom
-
00:41:58 catch-all roots at the bottom it could look like this okay so uh it is
-
00:42:01 it could look like this okay so uh it is important
-
00:42:02 important that uh the rules are processed from top
-
00:42:06 that uh the rules are processed from top to bottom
-
00:42:07 to bottom from the uh edit order
-
00:42:10 from the uh edit order therefore if we want one rule to
-
00:42:14 therefore if we want one rule to be processed earlier we have to put it
-
00:42:16 be processed earlier we have to put it to the top
-
00:42:17 to the top okay and if that rule matches no more
-
00:42:22 okay and if that rule matches no more uh rules will be checked if that route
-
00:42:24 uh rules will be checked if that route does not match it will continue
-
00:42:26 does not match it will continue uh to bottom to search until it finds a
-
00:42:30 uh to bottom to search until it finds a root
-
00:42:31 root that is important okay
-
00:42:38 so if we add you see currently it is looking for parameter paths
-
00:42:41 looking for parameter paths however if i add another rule here and
-
00:42:44 however if i add another rule here and another number two
-
00:42:46 another number two and instead of action parameter pass i
-
00:42:48 and instead of action parameter pass i will
-
00:42:50 will use index now this route will be
-
00:42:53 use index now this route will be used first because it is added first
-
00:42:57 used first because it is added first let's test it
-
00:43:28 okay and then i will add id you see it is being used not the other
-
00:43:31 you see it is being used not the other one is being used
-
00:43:32 one is being used so this one is being used first let's
-
00:44:26 okay so this happened this explanation is pretty
-
00:44:28 is pretty obvious okay
-
00:44:31 obvious okay so here it is showing the same thing
-
00:44:37 so here it is showing the same thing notice how i have a very specific rule
-
00:44:39 notice how i have a very specific rule first for products
-
00:44:40 first for products and then the default catch all rule in
-
00:44:42 and then the default catch all rule in the end
-
00:44:44 the end it makes sure that even if you haven't
-
00:44:45 it makes sure that even if you haven't defined a specific route to a controller
-
00:44:47 defined a specific route to a controller slash action combination
-
00:44:49 slash action combination it will try to reach it if it exists if
-
00:44:51 it will try to reach it if it exists if it doesn't exist
-
00:44:52 it doesn't exist it will fall back to the index method on
-
00:44:54 it will fall back to the index method on the home controller
-
00:44:56 the home controller okay we also uh used the default loading
-
00:44:59 okay we also uh used the default loading here
-
00:45:00 here so we have combined it with f use mvc
-
00:45:03 so we have combined it with f use mvc and app use roading how did we do it we
-
00:45:06 and app use roading how did we do it we did
-
00:45:07 did do it with options enabled and point
-
00:45:09 do it with options enabled and point running false
-
00:45:26 okay so now both is working both endpoints and default is being used okay
-
00:45:32 endpoints and default is being used okay let's see if there's anything important
-
00:45:58 home controller summary by now you should have a much better
-
00:46:00 by now you should have a much better idea about how routing works
-
00:46:02 idea about how routing works and how to define your own routes during
-
00:46:05 and how to define your own routes during the next articles we will look into more
-
00:46:07 the next articles we will look into more available options you have when defining
-
00:46:08 available options you have when defining your routes
-
00:46:09 your routes as well as other ways of using the
-
00:46:11 as well as other ways of using the routing mechanisms of asp.net mvc
-
00:46:15 routing mechanisms of asp.net mvc all right let's continue
-
00:46:25 routing attribute routing in the previous article we discussed
-
00:46:27 in the previous article we discussed what a root is how it works
-
00:46:29 what a root is how it works and how you can define roots in the
-
00:46:31 and how you can define roots in the startup.cs files
-
00:46:33 startup.cs files this is how roots are defined in a lot
-
00:46:35 this is how roots are defined in a lot of projects but there is an
-
00:46:37 of projects but there is an alternative asp.net mvc allows you to
-
00:46:40 alternative asp.net mvc allows you to define a root directly on the
-
00:46:41 define a root directly on the responsible controller and or
-
00:46:43 responsible controller and or specifically for the responsible action
-
00:46:45 specifically for the responsible action slash method
-
00:46:46 slash method this is often called attribute routing
-
00:46:49 this is often called attribute routing because you use attributes to define the
-
00:46:50 because you use attributes to define the root for the method
-
00:46:52 root for the method allow me to illustrate with an example
-
00:46:55 allow me to illustrate with an example okay so
-
00:46:56 okay so it is possible to define attributes such
-
00:46:59 it is possible to define attributes such as this one to
-
00:47:03 as this one to define rolling okay and
-
00:47:19 so uh for tests i will define an attribute loading
-
00:47:21 attribute loading so to attribute it starts with square
-
00:47:23 so to attribute it starts with square bracket
-
00:47:25 bracket then it gets a rod
-
00:47:29 then it gets a rod which is a special method you see
-
00:47:32 which is a special method you see and inside road it takes the path okay
-
00:47:35 and inside road it takes the path okay part of the rod
-
00:47:37 part of the rod so you see sitting template properties
-
00:47:41 so you see sitting template properties name orders
-
00:47:43 name orders okay so the template may be null i will
-
00:47:46 okay so the template may be null i will make the test as
-
00:47:48 make the test as okay test two
-
00:47:52 okay test two okay let's try it and see if test2
-
00:47:57 okay let's try it and see if test2 is going to test
-
00:48:11 okay however which one is first we process it
-
00:48:12 process it i don't know i think we should also test
-
00:48:15 i don't know i think we should also test it
-
00:48:22 okay and then test two yes it is working however if we add another uh
-
00:48:25 however if we add another uh route to our startup cs
-
00:48:29 route to our startup cs let's see let's see what happens okay
-
00:48:32 let's see let's see what happens okay so i will define the test
-
00:48:35 so i will define the test version 2 it will be test 2
-
00:48:39 version 2 it will be test 2 same but it will go to this one
-
00:48:42 same but it will go to this one let's see what happens now so this one
-
00:48:46 let's see what happens now so this one and that one here is conflicting
-
00:48:50 and that one here is conflicting so which route will be used
-
00:49:03 it will use the first matching one therefore i am
-
00:49:04 therefore i am assuming the one that we have defined in
-
00:49:07 assuming the one that we have defined in the startup cs will be used oh no
-
00:49:11 startup cs will be used oh no the one here is being used
-
00:49:15 the one here is being used uh okay so first it is checking
-
00:49:18 uh okay so first it is checking um the controller route it looks like
-
00:49:22 um the controller route it looks like not the one we defined in
-
00:49:26 not the one we defined in startup cs because it would call another
-
00:49:29 startup cs because it would call another one if
-
00:49:30 one if it was using this one
-
00:49:43 pay attention to the very first line of the example where i use the root
-
00:49:46 the example where i use the root attribute method to define a routing
-
00:49:47 attribute method to define a routing template for this controller
-
00:49:50 template for this controller you will notice that my controller uses
-
00:49:52 you will notice that my controller uses the word product in the singular form
-
00:49:54 the word product in the singular form but i would like to use the plural form
-
00:49:55 but i would like to use the plural form for the urls
-
00:49:57 for the urls i can accomplish just that by defining
-
00:49:59 i can accomplish just that by defining this word as the routing template for
-
00:50:01 this word as the routing template for the controller with that in place
-
00:50:02 the controller with that in place i can now access urls like slash
-
00:50:04 i can now access urls like slash products slasher slash products
-
00:50:07 products slasher slash products since they will just default to our
-
00:50:08 since they will just default to our index method
-
00:50:10 index method that was very basic and only works
-
00:50:12 that was very basic and only works because it automatically falls back to
-
00:50:14 because it automatically falls back to the index method
-
00:50:15 the index method but attribute routing allows for much
-
00:50:17 but attribute routing allows for much more complicated use cases as well
-
00:50:20 more complicated use cases as well for instance our controller could also
-
00:50:22 for instance our controller could also be used to serve a single product
-
00:50:24 be used to serve a single product instead of the general index slash
-
00:50:26 instead of the general index slash overview
-
00:50:27 overview let's add a method for that and define
-
00:50:29 let's add a method for that and define an access route for it at the same time
-
00:50:32 an access route for it at the same time okay so there is a road at the top of
-
00:50:35 okay so there is a road at the top of the
-
00:50:36 the class and then uh
-
00:50:39 class and then uh there is road optional id parameter
-
00:50:43 there is road optional id parameter id okay this looks much better
-
00:50:46 id okay this looks much better so to test it i will add another one
-
00:50:50 so to test it i will add another one let's add a controller as
-
00:50:54 let's add a controller as let's say attribute load
-
00:50:58 let's say attribute load attribute routing okay
-
00:51:07 and here i will add another wave as
-
00:51:13 it can be anything basically so i will just add a view here so we will
-
00:51:16 just add a view here so we will go out of the default template
-
00:51:19 go out of the default template let's say attributes okay whatever we
-
00:51:23 let's say attributes okay whatever we want
-
00:51:30 and here's okay attributes cs html
-
00:51:37 and you see this is not under any proper directory therefore i will return
-
00:51:41 proper directory therefore i will return a full patch like this
-
00:51:46 a full patch like this this is under wheels of course and here
-
00:51:49 this is under wheels of course and here i will define a route
-
00:51:51 i will define a route okay road will be let's say
-
00:51:54 okay road will be let's say this one will be
-
00:51:56 this one will be [Music]
-
00:52:00 [Music] okay items okay items
-
00:52:03 okay items okay items you see not related to attribute on
-
00:52:05 you see not related to attribute on anything it is just
-
00:52:06 anything it is just items and
-
00:52:09 items and oh we have to put a slash here by the
-
00:52:11 oh we have to put a slash here by the way i will make it here
-
00:52:13 way i will make it here and here i will add an id parameters
-
00:52:19 and here i will add an id parameters but
-
00:52:33 modules irv models and add
-
00:52:37 models and add a class as
-
00:52:43 uh product okay you see i am using all different names but
-
00:52:45 different names but if you
-
00:52:52 use them properly with the proper names it should still work i will have a
-
00:52:54 it should still work i will have a public integer
-
00:52:56 public integer let's say item id okay
-
00:53:00 let's say item id okay and here i will take another
-
00:53:03 and here i will take another road as parameters such as
-
00:53:07 road as parameters such as let's say
-
00:53:14 okay product i okay and um it will be the same as
-
00:53:20 um it will be the same as yes okay
-
00:53:24 yes okay one moment it will be integers in
-
00:53:28 one moment it will be integers in product
-
00:53:29 product id i will define a model start
-
00:53:33 id i will define a model start product okay my product equal to the
-
00:53:37 product okay my product equal to the product and my product dot
-
00:53:41 product and my product dot item id will be equal to
-
00:53:45 item id will be equal to product id and i will also return my
-
00:53:50 product id and i will also return my model
-
00:53:51 model okay like this and inside our view which
-
00:53:54 okay like this and inside our view which is attributes here i will define
-
00:53:55 is attributes here i will define my model equal to
-
00:54:00 my model equal to model start
-
00:54:33 product and now i can use attributes html
-
00:54:34 html and passwords item id will be equal
-
00:54:38 and passwords item id will be equal to its model
-
00:55:04 okay here's oh it's mine okay you see when we define our model we use
-
00:55:06 you see when we define our model we use smaller up
-
00:55:07 smaller up case first letter and then we access
-
00:55:09 case first letter and then we access model with uppercase
-
00:55:12 model with uppercase and let's run our application to see how
-
00:55:15 and let's run our application to see how it is working
-
00:55:27 so for attribute routing uh controller we have defined
-
00:55:28 we have defined the main route as items
-
00:55:32 the main route as items and then it will get product id root
-
00:55:35 and then it will get product id root like this
-
00:55:48 okay let's try items with item okay it is not working because it
-
00:55:52 item okay it is not working because it is expecting
-
00:55:54 is expecting also action name however when we type
-
00:55:56 also action name however when we type like this it is working why
-
00:55:58 like this it is working why because now instead of the action name
-
00:56:00 because now instead of the action name it is able to understand
-
00:56:03 it is able to understand this optional parameter routing by the
-
00:56:06 this optional parameter routing by the way it works with items but
-
00:56:07 way it works with items but it works with items as well yes
-
00:56:11 it works with items as well yes so both uppercase and lowercase is
-
00:56:13 so both uppercase and lowercase is working with the preset parameters
-
00:56:17 working with the preset parameters even if you do something like this works
-
00:56:19 even if you do something like this works if we do something like this
-
00:56:22 if we do something like this still work and like this not working
-
00:56:25 still work and like this not working so for this to work we need to provide
-
00:56:29 so for this to work we need to provide product id equal to
-
00:56:33 product id equal to oh product
-
00:56:36 oh product is still not working
-
00:56:45 yeah so we have to use it something like this so this is what
-
00:56:46 this so this is what okay let's add a comment here
-
00:56:51 okay let's add a comment here however if we define same thing
-
00:56:55 however if we define same thing okay like this then what happens
-
00:56:59 okay like this then what happens usually bought our product id i am
-
00:57:01 usually bought our product id i am assuming
-
00:57:02 assuming that uh the first one will be
-
00:57:06 that uh the first one will be called but it may also throw an
-
00:57:09 called but it may also throw an error let's see what happens
-
00:57:33 two yes we have an error it says that multiple actions match it
-
00:57:35 multiple actions match it okay uh the following action matches
-
00:57:39 okay uh the following action matches wrote data and had all constraints
-
00:57:42 wrote data and had all constraints satisfied
-
00:57:43 satisfied so you if you make like this you get an
-
00:57:45 so you if you make like this you get an error on the runtime okay
-
00:57:56 we can fix it with [Music]
-
00:57:59 [Music] additional requirement i think
-
00:58:08 such as perhaps like this let's put an a to the beginning i wonder if it
-
00:58:12 an a to the beginning i wonder if it will work
-
00:58:24 you see if you want to learn how does the system work you have to make
-
00:58:25 the system work you have to make experiments
-
00:58:26 experiments okay okay items
-
00:58:29 okay okay items a dot 32 yes now it is working
-
00:58:33 a dot 32 yes now it is working now it is going to this one you see how
-
00:58:36 now it is going to this one you see how it is
-
00:58:37 it is easy to use it is convenient to use
-
00:58:40 easy to use it is convenient to use okay great
-
00:58:50 okay let's continue to our tutorial notice how i have added a root attribute
-
00:58:52 notice how i have added a root attribute to the details method
-
00:58:54 to the details method it uses a special syntax for defining
-
00:58:56 it uses a special syntax for defining variable content
-
00:58:58 variable content where we surround the name of the
-
00:58:59 where we surround the name of the parameter with a set of curly braces
-
00:59:02 parameter with a set of curly braces we will discuss this more in depth later
-
00:59:04 we will discuss this more in depth later in this chapter but for now
-
00:59:06 in this chapter but for now just be aware that it allows us to
-
00:59:07 just be aware that it allows us to specify a parameter which will be passed
-
00:59:09 specify a parameter which will be passed to the responding method
-
00:59:11 to the responding method what it means in this case is that we
-
00:59:13 what it means in this case is that we could now load the product with the id
-
00:59:15 could now load the product with the id pass through the url
-
00:59:16 pass through the url and display it to the user in the
-
00:59:18 and display it to the user in the example we just print the id
-
00:59:20 example we just print the id though to keep things simple our
-
00:59:23 though to keep things simple our controller now supports urls like this
-
00:59:25 controller now supports urls like this one slash products
-
00:59:26 one slash products slash forty two and slash products slash
-
00:59:29 slash forty two and slash products slash eighty four slash
-
00:59:30 eighty four slash so the last slash is optional
-
00:59:33 so the last slash is optional please notice that i only use the last
-
00:59:35 please notice that i only use the last part of the url in the root template
-
00:59:37 part of the url in the root template the id because the controller already
-
00:59:40 the id because the controller already specifies that it should be called with
-
00:59:41 specifies that it should be called with the word products first
-
00:59:43 the word products first if that wasn't the case we would have to
-
00:59:45 if that wasn't the case we would have to define the entire url for this specific
-
00:59:48 define the entire url for this specific method instead like this
-
00:59:50 method instead like this okay why we don't you put the products
-
00:59:52 okay why we don't you put the products in the beginning
-
00:59:53 in the beginning is because we have defined the default
-
00:59:56 is because we have defined the default here
-
00:59:57 here therefore i didn't have to put items
-
01:00:00 therefore i didn't have to put items here and when we go to items now
-
01:00:04 here and when we go to items now directory it will go to this class
-
01:00:08 directory it will go to this class okay this controller and everything
-
01:00:11 okay this controller and everything every method inside this will take
-
01:00:15 every method inside this will take this by default okay that's how it is
-
01:00:24 so these items is coming from here okay okay let's continue
-
01:00:28 okay okay let's continue so whether you define it partially for
-
01:00:30 so whether you define it partially for the controller and partially for the
-
01:00:31 the controller and partially for the method or specify it all on each method is
-
01:00:34 or specify it all on each method is really up to you
-
01:00:35 really up to you and the situation okay let's try another
-
01:00:38 and the situation okay let's try another one another case and to show you how
-
01:00:42 another case and to show you how how it works so i will call this as b
-
01:00:46 how it works so i will call this as b but i will provide a full path okay such
-
01:00:48 but i will provide a full path okay such as items
-
01:00:51 as items and it will be index three and this time
-
01:00:56 and it will be index three and this time i will divide it by five
-
01:01:11 this should also work because now we are providing full
-
01:01:12 providing full path okay
-
01:01:29 okay items be 100 e
-
01:01:38 okay so it's not working why
-
01:01:53 oh which method action is being called
-
01:02:09 why because this catches every [Music]
-
01:02:11 [Music] everything that doesn't match
-
01:02:16 everything that doesn't match the path
-
01:02:22 i think i know the reason because i have to put
-
01:02:22 to put another slash here now it won't catch it
-
01:02:26 another slash here now it won't catch it you see we have just learned something
-
01:02:28 you see we have just learned something else as well
-
01:02:39 so this slash means that it will go to the root of the
-
01:02:40 the root of the website of the url which is this one in
-
01:02:43 website of the url which is this one in this case
-
01:02:50 there is nothing supporting https which is we allow
-
01:02:51 is we allow [Music]
-
01:02:53 [Music] p100 yes now this is working
-
01:02:56 p100 yes now this is working okay
-
01:03:08 all right now it is now we understand how it works
-
01:03:16 situation it could be useful to define it all on each method if your controller
-
01:03:18 it all on each method if your controller should support more than one entrance
-
01:03:20 should support more than one entrance url
-
01:03:20 url but in most situations you could
-
01:03:22 but in most situations you could probably save a few keystrokes by
-
01:03:24 probably save a few keystrokes by defining a common entrance for the
-
01:03:26 defining a common entrance for the controller and then the rest for each
-
01:03:27 controller and then the rest for each method
-
01:03:29 method for some situations you may want a
-
01:03:31 for some situations you may want a method to have more than one root
-
01:03:33 method to have more than one root this is easily accomplished because you
-
01:03:35 this is easily accomplished because you can define as many as you want
-
01:03:38 can define as many as you want you can even mix and match urls
-
01:03:40 you can even mix and match urls partially and completely defined on the
-
01:03:42 partially and completely defined on the specific method like this
-
01:03:44 specific method like this okay this is uh another great uh thing
-
01:03:47 okay this is uh another great uh thing so let me show you
-
01:03:56 let's add another one index 4 whatever the name we want
-
01:04:02 it doesn't matter because we are using routing and here
-
01:04:04 routing and here i will define three cases
-
01:04:07 i will define three cases and so let's provide full path
-
01:04:11 and so let's provide full path product
-
01:04:18 and optional parameters product id okay and
-
01:04:23 okay and define another path as
-
01:04:26 define another path as item product id so
-
01:04:30 item product id so what should work and
-
01:04:34 what should work and this time i will return to
-
01:04:36 this time i will return to multiplication
-
01:04:38 multiplication of three times
-
01:04:45 okay let's try so we have defined two different routings as you can see
-
01:04:48 different routings as you can see these two are different uh i will add
-
01:04:52 these two are different uh i will add them as a command to do here
-
01:04:55 them as a command to do here in a moment
-
01:05:04 okay so let's try the first one items and then product
-
01:05:07 and then product and then 32 okay it is working
-
01:05:22 item and product oh it didn't work oh it should work well
-
01:05:25 oh it didn't work oh it should work well i know the reason because we have to
-
01:05:26 i know the reason because we have to still have items packed
-
01:05:36 you see these are different items product items item
-
01:05:37 product items item and board is displaying the same okay
-
01:05:42 and board is displaying the same okay all right so we will continue the
-
01:05:46 all right so we will continue the moments
-
01:05:47 moments one second
-
01:05:59 with that in place our product details page can now be accessed using urls with
-
01:06:02 page can now be accessed using urls with both the singular
-
01:06:03 both the singular and the plural form of the word product
-
01:06:06 and the plural form of the word product this specific result could be
-
01:06:07 this specific result could be accomplished in other ways
-
01:06:09 accomplished in other ways with a more flexible root but it serves
-
01:06:11 with a more flexible root but it serves as a nice demonstration of how easy it
-
01:06:13 as a nice demonstration of how easy it is to define multiple roots
-
01:06:16 is to define multiple roots summary attribute routing can be a great
-
01:06:20 summary attribute routing can be a great alternative to the conventional way of
-
01:06:21 alternative to the conventional way of defining routes in your project
-
01:06:24 defining routes in your project because they are defined directly on the
-
01:06:26 because they are defined directly on the controller it can be easier to
-
01:06:28 controller it can be easier to understand which routes are valid for a
-
01:06:29 understand which routes are valid for a given controller and method
-
01:06:32 given controller and method on the other hand there are a few
-
01:06:33 on the other hand there are a few limitations when using attribute routing
-
01:06:36 limitations when using attribute routing when it comes to routing constraints but
-
01:06:37 when it comes to routing constraints but for most situations
-
01:06:39 for most situations this shouldn't be a problem if you run
-
01:06:41 this shouldn't be a problem if you run into these limitations anyway
-
01:06:43 into these limitations anyway remember that you are free to combine
-
01:06:45 remember that you are free to combine conventional routing with attribute
-
01:06:47 conventional routing with attribute routing
-
01:06:49 routing so at the end of the day whether to use
-
01:06:51 so at the end of the day whether to use a tribute routing or not comes down to
-
01:06:53 a tribute routing or not comes down to personal taste
-
01:06:54 personal taste and the requirements of the project you
-
01:06:55 and the requirements of the project you are working on
-
01:06:57 are working on so let's compare them
-
01:07:14 okay i feel someone has already asked this
-
01:07:33 yeah let's read this page i think it will be a useful one introduction
-
01:07:44 will be a useful one introduction routing is the first and foremost
-
01:07:46 routing is the first and foremost phenomenon in the asp.net mvc pipeline
-
01:07:50 phenomenon in the asp.net mvc pipeline here you will learn about routing how it
-
01:07:52 here you will learn about routing how it works and its variants
-
01:07:55 works and its variants background last week one of my friends
-
01:07:58 background last week one of my friends asked this question
-
01:07:59 asked this question can we make our custom route with some
-
01:08:01 can we make our custom route with some constraints and what is a tribute
-
01:08:03 constraints and what is a tribute routing
-
01:08:04 routing i am dedicating this article to him i
-
01:08:07 i am dedicating this article to him i hope he will like this
-
01:08:09 hope he will like this the topics to be covered are routing and
-
01:08:12 the topics to be covered are routing and how it works
-
01:08:15 how it works parts of a root default root in custom
-
01:08:18 parts of a root default root in custom root
-
01:08:20 root what is the purpose ignore root method
-
01:08:23 what is the purpose ignore root method how to apply constraints to custom root
-
01:08:26 how to apply constraints to custom root what is attribute routing default
-
01:08:29 what is attribute routing default and optional parameter default action in
-
01:08:33 and optional parameter default action in root prefix
-
01:08:35 root prefix name and order property attribute
-
01:08:38 name and order property attribute routing including constraints
-
01:08:41 routing including constraints routing routing is the first step in
-
01:08:44 routing routing is the first step in asp.net nvc pipeline
-
01:08:47 asp.net nvc pipeline this is the replacement of the concrete
-
01:08:50 this is the replacement of the concrete physical files used in the u
-
01:08:52 physical files used in the u okay so it stops all files
-
01:08:57 okay so it stops all files why do we need routing
-
01:09:04 in other words routing is the phenomenon in which controller and actions execute
-
01:09:06 in which controller and actions execute rather than the concrete physical files
-
01:09:09 rather than the concrete physical files why do we need routing traditionally
-
01:09:12 why do we need routing traditionally the url of the browser represents the
-
01:09:14 the url of the browser represents the physical file
-
01:09:16 physical file a file can be html file aspx page etc
-
01:09:21 a file can be html file aspx page etc let's understand this traditional way
-
01:09:28 www.abcd.com images slash john dot aspx question mark
-
01:09:31 images slash john dot aspx question mark id equal sign two this url
-
01:09:34 id equal sign two this url shows that first we go to images folder
-
01:09:36 shows that first we go to images folder then access the john.aspx file having
-
01:09:39 then access the john.aspx file having ids too
-
01:09:41 ids too yes this is the traditional way
-
01:09:44 yes this is the traditional way it goes to this directory then inside it
-
01:09:47 it goes to this directory then inside it it goes to john.aspx file
-
01:09:50 it goes to john.aspx file with the additional parameter id equal
-
01:09:52 with the additional parameter id equal to okay
-
01:09:53 to okay so this is it two hence it is not the
-
01:09:56 so this is it two hence it is not the good way to expose your precious
-
01:09:58 good way to expose your precious information to the urls of the browser
-
01:10:00 information to the urls of the browser so with these url sites can be hacked
-
01:10:03 so with these url sites can be hacked as a result routing comes into action
-
01:10:06 as a result routing comes into action and solves this problem of the
-
01:10:07 and solves this problem of the traditional file based system
-
01:10:10 traditional file based system implementation of routing starts with
-
01:10:12 implementation of routing starts with root table
-
01:10:13 root table the root table is a collection of all
-
01:10:15 the root table is a collection of all possible correct routes that can be used
-
01:10:17 possible correct routes that can be used to map the http request urls
-
01:10:21 to map the http request urls let's understand root table and the
-
01:10:22 let's understand root table and the working of routing in detail
-
01:10:25 working of routing in detail routing is a pattern matching system
-
01:10:27 routing is a pattern matching system that matches the incoming request to the
-
01:10:29 that matches the incoming request to the registered url patterns reside in the
-
01:10:31 registered url patterns reside in the root table
-
01:10:33 root table when an asp.net mvc application starts
-
01:10:36 when an asp.net mvc application starts it registers patterns to the root table
-
01:10:38 it registers patterns to the root table to tell the routing engine to give a
-
01:10:40 to tell the routing engine to give a response to the requests that match
-
01:10:42 response to the requests that match these patterns
-
01:10:43 these patterns an application has only one root table
-
01:10:46 an application has only one root table and it resides in the application
-
01:10:47 and it resides in the application underscore start event of global.asax of
-
01:10:50 underscore start event of global.asax of the application
-
01:10:51 the application the routes are registered to the root
-
01:10:53 the routes are registered to the root config class that has the root urls to
-
01:10:55 config class that has the root urls to be mapped and ignored in the application
-
01:10:58 be mapped and ignored in the application by the way this is
-
01:11:07 no okay okay there is nothing yet application
-
01:11:09 application let's have a look at it public classroom
-
01:11:12 let's have a look at it public classroom okay here you see they have added
-
01:11:16 okay here you see they have added resources.axtx to be
-
01:11:18 resources.axtx to be ignored in roading because this is uh
-
01:11:20 ignored in roading because this is uh usually files provided by
-
01:11:23 usually files provided by asp.net and here the
-
01:11:26 asp.net and here the default and whatever it is and global a6
-
01:11:28 default and whatever it is and global a6 we don't have global a6 because our
-
01:11:30 we don't have global a6 because our application is
-
01:11:32 application is and we see dot net core not that net
-
01:11:35 and we see dot net core not that net framework in dotnet framework we have a
-
01:11:37 framework in dotnet framework we have a global acx but here we don't have it
-
01:11:41 global acx but here we don't have it but the roading probably looks in the
-
01:11:43 but the roading probably looks in the same way
-
01:11:50 okay however i wanted to see the comparison of
-
01:12:03 the comparison of attribute loading and traditional routing
-
01:12:09 okay here's yeah let's continue from here
-
01:12:11 here what is attribute routing attribute
-
01:12:14 what is attribute routing attribute routing is the new type of routing in
-
01:12:16 routing is the new type of routing in asp.net nvc5
-
01:12:19 asp.net nvc5 according to the name attribute routing
-
01:12:21 according to the name attribute routing one can suppose that this type of
-
01:12:23 one can suppose that this type of methodology will use a tribute to define
-
01:12:25 methodology will use a tribute to define roots
-
01:12:26 roots yes let's understand it in detail
-
01:12:31 yes let's understand it in detail why do we need attribute routing
-
01:12:34 why do we need attribute routing first of all look at our previous custom
-
01:12:36 first of all look at our previous custom route
-
01:12:38 route public okay so each time
-
01:12:41 public okay so each time they write wrote my approach i see
-
01:12:49 we have seen this already your apartment okay so this is
-
01:12:50 okay so this is kind of older quote
-
01:12:58 in the above code there are only two routes defined with the help of maproot
-
01:13:00 routes defined with the help of maproot but when our application is large enough
-
01:13:02 but when our application is large enough to have more than two roots
-
01:13:04 to have more than two roots then it will become very complex to make
-
01:13:06 then it will become very complex to make each root separately
-
01:13:08 each root separately for each root you have to write five
-
01:13:10 for each root you have to write five lines so it will consume your time
-
01:13:12 lines so it will consume your time and disturb your development time you
-
01:13:15 and disturb your development time you have to take care of the url pattern in
-
01:13:17 have to take care of the url pattern in root config class
-
01:13:18 root config class as well as in controller where your
-
01:13:20 as well as in controller where your request will be handled
-
01:13:22 request will be handled so this to and fro movement in between
-
01:13:24 so this to and fro movement in between these folders creates complexity for you
-
01:13:27 these folders creates complexity for you it will reduce the chances of errors
-
01:13:29 it will reduce the chances of errors because in root config class there can
-
01:13:31 because in root config class there can be any mistake while creating a new
-
01:13:33 be any mistake while creating a new custom route
-
01:13:35 custom route it is easy to use and help in mapping
-
01:13:37 it is easy to use and help in mapping the same action method with two routes
-
01:13:40 the same action method with two routes you don't have to take care of the
-
01:13:41 you don't have to take care of the routing flow i.e from most specific to
-
01:13:44 routing flow i.e from most specific to most general
-
01:13:45 most general all here is the attribute you use the
-
01:13:47 all here is the attribute you use the action method
-
01:13:49 action method now let's move to use attribute routing
-
01:13:52 now let's move to use attribute routing first of all you have to enable the
-
01:13:54 first of all you have to enable the access to attribute routing
-
01:13:57 access to attribute routing so root config class becomes okay our
-
01:14:01 so root config class becomes okay our application already has that features
-
01:14:10 okay so this is the way we okay we can also define default parameters like this
-
01:14:15 also define default parameters like this and there is road prefix
-
01:14:27 okay oh there is also road order i think you will see that
-
01:14:34 let's continue through our tutorial
-
01:14:50 routing templates so far the roots we have used have been very
-
01:14:52 the roots we have used have been very basic a static path
-
01:14:53 basic a static path sometimes combined with a simple
-
01:14:55 sometimes combined with a simple variable parameter
-
01:14:57 variable parameter however the routing system in asp.net
-
01:15:00 however the routing system in asp.net mvc has a lot more to offer
-
01:15:03 mvc has a lot more to offer you can in fact create very advanced url
-
01:15:05 you can in fact create very advanced url routes
-
01:15:06 routes using root templates we'll look into
-
01:15:09 using root templates we'll look into some of the possibilities in this
-
01:15:10 some of the possibilities in this article
-
01:15:12 article in a previous article we used the id
-
01:15:15 in a previous article we used the id syntax to map a part of the url to a
-
01:15:17 syntax to map a part of the url to a parameter called id
-
01:15:19 parameter called id in the receiving controller method this
-
01:15:21 in the receiving controller method this is one of the most basic routing
-
01:15:23 is one of the most basic routing templates you can
-
01:15:23 templates you can use but you are not limited to using
-
01:15:26 use but you are not limited to using just one variable slash parameter in
-
01:15:28 just one variable slash parameter in your roots
-
01:15:29 your roots in fact you can use as many as you want
-
01:15:31 in fact you can use as many as you want and you are free to combine them in any
-
01:15:33 and you are free to combine them in any way you see fit
-
01:15:35 way you see fit this could for example be used to create
-
01:15:37 this could for example be used to create a url with both an id
-
01:15:39 a url with both an id and a slug as seen in many blog systems
-
01:15:42 and a slug as seen in many blog systems like wordpress etc
-
01:15:44 like wordpress etc here's an example of a routing template
-
01:15:46 here's an example of a routing template where we combine two parameters to
-
01:15:48 where we combine two parameters to create the classical
-
01:15:49 create the classical id slash url underscore slug url
-
01:15:53 id slash url underscore slug url okay let's uh define this uh
-
01:15:57 okay let's uh define this uh so i will copy and paste this into
-
01:16:00 so i will copy and paste this into my code you see it will use block
-
01:16:03 my code you see it will use block as a root and however i want to make it
-
01:16:07 as a root and however i want to make it the
-
01:16:07 the uh root by block not the items therefore
-
01:16:11 uh root by block not the items therefore i define full path
-
01:16:13 i define full path as like this and i enter it slug
-
01:16:17 as like this and i enter it slug so in my model
-
01:16:20 so in my model which is here attribute products model i
-
01:16:23 which is here attribute products model i am going to add a slack parameter as
-
01:16:25 am going to add a slack parameter as well
-
01:16:26 well okay let's define it as property
-
01:16:43 we will return not the content but the product
-
01:16:45 product so our weave like here actually
-
01:16:50 so our weave like here actually and entry id will be id
-
01:16:54 and entry id will be id and the slack will be
-
01:17:14 until you see us html i will put some hr like this
-
01:17:28 and let's say block id equal to and modeled
-
01:17:45 and the slack will be equal to what a dot slack and let's run the
-
01:17:48 what a dot slack and let's run the application
-
01:17:50 application so you see we are able to set
-
01:17:53 so you see we are able to set a loading pad that is completely
-
01:17:56 a loading pad that is completely irrelevant to
-
01:17:58 irrelevant to our class names method names action
-
01:18:01 our class names method names action names whatever
-
01:18:03 names whatever it is totally possible i could also set
-
01:18:06 it is totally possible i could also set this in startup cs but i think it is better
-
01:18:10 in startup cs but i think it is better to set it here
-
01:18:12 to set it here because when you check the method you
-
01:18:14 because when you check the method you can directly see
-
01:18:16 can directly see what kind of path it will be
-
01:18:19 what kind of path it will be using and here
-
01:18:22 using and here i will set it as block
-
01:18:26 i will set it as block and id let's say 500 and slot let's say
-
01:18:31 and id let's say 500 and slot let's say routing is great in asu.net
-
01:18:35 routing is great in asu.net you see it is working as expected block
-
01:18:38 you see it is working as expected block id 500 block slot
-
01:18:39 id 500 block slot is routing is great in the asp.net i
-
01:18:42 is routing is great in the asp.net i think i can also put an
-
01:18:43 think i can also put an ending slash here yes still working so
-
01:18:46 ending slash here yes still working so either way
-
01:18:47 either way is working as expected
-
01:18:55 okay and it usually it says you can now reach
-
01:18:57 and it usually it says you can now reach this action using url like this
-
01:19:01 this action using url like this so basically what does this city is
-
01:19:03 so basically what does this city is using is
-
01:19:04 using is it has registering routing templates
-
01:19:07 it has registering routing templates and the routing to an id of the article
-
01:19:11 and the routing to an id of the article so when these routing templates comes it
-
01:19:14 so when these routing templates comes it reads that parameter
-
01:19:16 reads that parameter and find the id of this article in the
-
01:19:19 and find the id of this article in the database
-
01:19:20 database and then it returns that article to our
-
01:19:24 and then it returns that article to our screen okay
-
01:19:35 153 will then be mapped to the entry id parameter
-
01:19:36 parameter while testing asp mvc roots will be
-
01:19:38 while testing asp mvc roots will be mapped to the slug parameter and based
-
01:19:40 mapped to the slug parameter and based on that
-
01:19:41 on that the following output will be generated
-
01:19:42 the following output will be generated by our action method
-
01:19:45 by our action method so method so you log it so you see it
-
01:19:48 so method so you log it so you see it doesn't have to be
-
01:19:49 doesn't have to be an idea this can be also mapped to
-
01:19:52 an idea this can be also mapped to the idea of
-
01:19:58 article how can that be done let's assume that we have a
-
01:20:01 assume that we have a database okay i will show you what does
-
01:20:04 database okay i will show you what does that mean
-
01:20:06 that mean let's say we have database class
-
01:20:10 let's say we have database class currently i won't be using a real
-
01:20:13 currently i won't be using a real database
-
01:20:14 database it's a later thing let's say
-
01:20:17 it's a later thing let's say articles or let's call it sdv
-
01:20:22 articles or let's call it sdv okay db class it is in our
-
01:20:25 okay db class it is in our entire folder like this i will make it
-
01:20:29 entire folder like this i will make it public static
-
01:20:31 public static and here i will have a
-
01:20:34 and here i will have a public static dictionary it will have
-
01:20:38 public static dictionary it will have string
-
01:20:38 string and string
-
01:20:48 okay and i will name it as dictionary articles okay so i will also have a
-
01:20:51 articles okay so i will also have a study constructor
-
01:20:54 study constructor db this is the constructor of the
-
01:20:58 db this is the constructor of the db class what this constructor going to
-
01:21:01 db class what this constructor going to do is it will
-
01:21:02 do is it will compose a new dictionary and then i will
-
01:21:05 compose a new dictionary and then i will add the articles
-
01:21:07 add the articles okay so the articles will be
-
01:21:10 okay so the articles will be added by the way i will make this uh as
-
01:21:13 added by the way i will make this uh as a tuple
-
01:21:13 a tuple actually not a string double
-
01:21:18 actually not a string double string and string
-
01:21:34 okay let's add this as an article okay so how it is going to work is
-
01:21:36 okay so how it is going to work is simply
-
01:21:44 let's get the page source of this page like here and
-
01:22:03 i will add it to a weave such as at weave and
-
01:22:06 at weave and let's name it as rooting html
-
01:22:11 let's name it as rooting html okay
-
01:22:20 yeah it should work writing html i am adding it the source code to this
-
01:22:23 i am adding it the source code to this page
-
01:22:25 page and i am going to add this edit with
-
01:22:29 and i am going to add this edit with new key volume
-
01:22:32 new key volume so it will be new tuple and it will take
-
01:22:36 so it will be new tuple and it will take item one item one will be
-
01:22:51 item two will be uh routing components okay
-
01:23:01 and the content the value will be routing html okay
-
01:23:09 now i'm going to have a public static return content method
-
01:23:13 return content method it will take string srt1
-
01:23:23 k2 and it will return dictionary articles uh one second
-
01:23:28 articles uh one second okay and it will return the article
-
01:23:32 okay and it will return the article name so we can make this private as well
-
01:23:36 name so we can make this private as well we don't need it so if
-
01:23:40 we don't need it so if let's compose the key as
-
01:23:43 let's compose the key as uh tuple
-
01:23:51 key equal to leave doubles sr1 2
-
01:23:55 sr1 2 and if dictionary articles contains key
-
01:23:59 and if dictionary articles contains key your key it will return okay
-
01:24:03 your key it will return okay you should have articles we are key
-
01:24:07 you should have articles we are key us we will return
-
01:24:10 us we will return um no
-
01:24:18 and uh simply we will call this method in our controller okay so
-
01:24:23 our controller okay so this is our type i'm going to
-
01:24:28 this is our type i'm going to use this or let's
-
01:24:32 use this or let's change it a bit and three
-
01:24:40 and it will use entry or let's say article
-
01:24:42 article and it will have entry id and slack or
-
01:24:45 and it will have entry id and slack or whatever
-
01:24:45 whatever it can be g1 and q2 whatever we want
-
01:24:50 it can be g1 and q2 whatever we want so by the way they just need to match
-
01:24:52 so by the way they just need to match here okay so it will be string string
-
01:24:56 here okay so it will be string string and uh we will get uh
-
01:24:59 and uh we will get uh where we are key equal or let's say
-
01:25:01 where we are key equal or let's say value equal to
-
01:25:04 value equal to our class name is uh db dot
-
01:25:08 our class name is uh db dot uh return content key k1
-
01:25:11 uh return content key k1 k2 and if
-
01:25:15 k2 and if string is
-
01:25:22 known or empty we are value we will return content
-
01:25:27 we will return content uh no such
-
01:25:30 uh no such article is found okay
-
01:25:34 article is found okay as uh we are going to return
-
01:25:37 as uh we are going to return we have the viewport pad
-
01:25:41 we have the viewport pad will be it is under weaves folders
-
01:25:44 will be it is under weaves folders and simply like this
-
01:25:48 and simply like this we will append the value and nothing
-
01:25:50 we will append the value and nothing else okay so let's
-
01:25:52 else okay so let's try it so article k1 k2
-
01:25:56 try it so article k1 k2 and we will have it
-
01:26:05 you can imagine this same thing in your database design with the input
-
01:26:08 database design with the input parameters such as this one then you can
-
01:26:11 parameters such as this one then you can check the article in the database and
-
01:26:13 check the article in the database and return it okay so it will be article
-
01:26:16 return it okay so it will be article one and one
-
01:26:19 one and one you see no such article is found then
-
01:26:22 you see no such article is found then let's use our keys
-
01:26:23 let's use our keys which are rooting
-
01:26:27 which are rooting okay uh the first key will be rooting
-
01:26:31 okay uh the first key will be rooting and the second key will be rooting
-
01:26:32 and the second key will be rooting temples okay
-
01:26:34 temples okay by the way you you cannot allow a slash
-
01:26:37 by the way you you cannot allow a slash character in your
-
01:26:39 character in your uh case other otherwise it would it can
-
01:26:42 uh case other otherwise it would it can break your application
-
01:26:44 break your application and now okay it says this rooting html
-
01:26:47 and now okay it says this rooting html was not found
-
01:26:50 was not found providing it related incorrectly
-
01:27:43 okay this is routing html let's rename to csh them and maybe maybe
-
01:27:46 let's rename to csh them and maybe maybe it is why
-
01:27:53 okay rooting cshtml and i will change it let's retry
-
01:27:57 and i will change it let's retry it should still work you know cshtml is
-
01:28:00 it should still work you know cshtml is a special type but you can
-
01:28:02 a special type but you can directly write as you are writing
-
01:28:13 maybe we need another configuration to return directly html that is why
-
01:28:24 okay also [Music]
-
01:28:26 [Music] um
-
01:28:29 um okay okay and then one
-
01:28:33 okay okay and then one one okay and
-
01:28:37 one okay and let's try our keys
-
01:28:42 let's try our keys routing and losing templates
-
01:28:54 okay we use routing did we write the name incorrectly
-
01:29:03 the following locations were searched weaves wrote in cshtml yes it is here
-
01:29:11 oh this is under users i have to put it inside here
-
01:29:12 inside here also it says same
-
01:29:21 oh interestingly yes it is here it is other waves
-
01:29:39 no we are typing it correctly
-
01:29:52 we are typing it correctly i don't understand why it is not working
-
01:29:55 i don't understand why it is not working let's try like this first and try to
-
01:29:58 let's try like this first and try to [Music]
-
01:30:00 [Music] figure out our problem
-
01:31:03 maybe maybe have a path that is blocking
-
01:31:07 that is blocking or
-
01:31:13 we may have a rule that is blocking to call our page
-
01:31:17 call our page so let's try to figure out what is wrong
-
01:32:24 this one working but the other one is not i don't understand why
-
01:32:36 yes this is working as expected but this one is not
-
01:32:59 what is the difference between this one and this one
-
01:33:09 let's add another cs html maybe because of that
-
01:33:22 okay this is the article cshtml and i will copy and paste this into
-
01:33:24 will copy and paste this into let's say article and let's try to
-
01:33:28 let's say article and let's try to return articles html
-
01:33:30 return articles html and try to figure out where
-
01:33:33 and try to figure out where we are making
-
01:33:36 we are making error
-
01:33:45 here it will return the value yes and
-
01:33:48 yes and here we will return
-
01:34:32 okay article one one and okay now i'm working so
-
01:34:40 basically uh something here probably throwing an error and what can
-
01:34:42 probably throwing an error and what can it be
-
01:34:43 it be so it's something here is
-
01:34:47 so it's something here is breaking our code obviously
-
01:34:56 i will copy and paste this to here and see what happens
-
01:35:00 see what happens let's just restart
-
01:35:10 so spread something we paste here is breaking us
-
01:35:15 breaking us caught this this source as you know
-
01:35:18 caught this this source as you know source code
-
01:35:27 let's see okay now working this is so weird
-
01:35:28 weird yes okay as you can see uh
-
01:35:32 yes okay as you can see uh this is the way that you can bring any
-
01:35:34 this is the way that you can bring any article you want in your
-
01:35:37 article you want in your database however i am
-
01:35:41 database however i am still confusing why rooting csa
-
01:35:43 still confusing why rooting csa statement is not
-
01:35:46 statement is not working maybe it is because it has
-
01:35:49 working maybe it is because it has rooting word in it in it i don't know
-
01:36:01 oh maybe it needs this yes it maybe needed this at the beginning
-
01:36:03 needed this at the beginning this one let's put this to the
-
01:36:26 okay this is so based as you can see this is not processed as a
-
01:36:37 laser syntax by cs html and this is also cs html
-
01:36:41 and this is also cs html so why
-
01:36:51 i think it is related to something that is automatically generated
-
01:36:54 is automatically generated let's try to find it
-
01:37:36 okay so this page is not being seen as a
-
01:37:54 maybe inside no not there is nothing simply
-
01:38:03 okay interesting very interesting okay so uh i have noticed
-
01:38:06 okay so uh i have noticed something while checking the reason why
-
01:38:11 something while checking the reason why rotting cshml might not working
-
01:38:14 rotting cshml might not working so here there is something as you can
-
01:38:17 so here there is something as you can see build action you see it is none
-
01:38:21 build action you see it is none but when we check the other other things
-
01:38:24 but when we check the other other things you see
-
01:38:24 you see build action content this is a really
-
01:38:28 build action content this is a really important part in the configuration
-
01:38:32 important part in the configuration so i'm going to make this as a content
-
01:38:34 so i'm going to make this as a content as well
-
01:38:36 as well and now let's
-
01:38:39 and now let's add another
-
01:38:42 add another um option here
-
01:38:47 um option here to our database
-
01:38:59 routing road v2 okay and this will go to routing article
-
01:39:10 here and now let's retry by the way uh for this uh
-
01:39:14 by the way uh for this uh to be seen as with
-
01:39:17 to be seen as with as razer page from
-
01:39:20 as razer page from intellisense we have to restart
-
01:39:22 intellisense we have to restart application
-
01:39:24 application closing and opening this page might help
-
01:39:28 closing and opening this page might help but i'm not sure
-
01:39:36 yes closing and opening that page also refreshes the cage
-
01:39:37 refreshes the cage and now we can see this page as a
-
01:39:41 and now we can see this page as a razer page as well and now
-
01:39:45 razer page as well and now let's see rooting of article
-
01:39:54 and rooting or what was our case
-
01:40:08 okay rooting v2 okay and now it is working as expected
-
01:40:13 and now it is working as expected okay so we have also solved that problem
-
01:40:16 okay so we have also solved that problem and now we can continue
-
01:40:22 and now we can continue catch all parameters if you test the
-
01:40:25 catch all parameters if you test the above route you will notice that your
-
01:40:27 above route you will notice that your slug can't contain forward slashes
-
01:40:29 slug can't contain forward slashes slash like this one slash blog slash
-
01:40:33 slash like this one slash blog slash 153 slash testing the slash routing
-
01:40:36 153 slash testing the slash routing system slash
-
01:40:38 system slash the reason is probably obvious the
-
01:40:40 the reason is probably obvious the forward slash
-
01:40:41 forward slash is normally used in urls to separate
-
01:40:43 is normally used in urls to separate folder names
-
01:40:44 folder names and even though your root doesn't
-
01:40:45 and even though your root doesn't necessarily relate to a specific folder
-
01:40:48 necessarily relate to a specific folder the forward slash still has a special
-
01:40:50 the forward slash still has a special meaning
-
01:40:51 meaning in this case it would seem that you are
-
01:40:53 in this case it would seem that you are trying to reach either two folders
-
01:40:55 trying to reach either two folders or two parameters with the values
-
01:40:56 or two parameters with the values testing the and routing system
-
01:40:59 testing the and routing system this might not be what you want though
-
01:41:01 this might not be what you want though perhaps in your system
-
01:41:02 perhaps in your system it makes sense to allow forward slashes
-
01:41:04 it makes sense to allow forward slashes as part of the url slug
-
01:41:06 as part of the url slug for situations like this asp.net mvc
-
01:41:10 for situations like this asp.net mvc includes the so-called catch-all
-
01:41:12 includes the so-called catch-all parameters
-
01:41:14 parameters a catch-all parameter is created by
-
01:41:16 a catch-all parameter is created by prefixing the name of the parameter with
-
01:41:18 prefixing the name of the parameter with an asterisk
-
01:41:19 an asterisk asterisk character like this okay so we
-
01:41:22 asterisk character like this okay so we can also use
-
01:41:23 can also use aesthetics character to catch all okay
-
01:41:26 aesthetics character to catch all okay and in that case
-
01:41:27 and in that case we would be able to use even this
-
01:41:30 we would be able to use even this special character as well
-
01:41:33 special character as well so let's see
-
01:41:48 so this will be the second example and
-
01:42:07 okay after adding that i should be able to add
-
01:42:08 to add whatever i want to the end of the
-
01:42:12 whatever i want to the end of the url let's test it
-
01:43:04 i see i see okay so this is not a format i think we have to make it
-
01:43:08 i think we have to make it like this let's retry
-
01:43:59 i think we need to make this as like this so it will allow everything has to be
-
01:44:03 so it will allow everything has to be yes so this is probably the correct way
-
01:44:12 and to show how it works let's add another volume
-
01:44:15 let's add another volume okay here's to our
-
01:44:19 okay here's to our database
-
01:44:31 rooting v2 okay now let's retry this is a forward slash therefore
-
01:44:33 this is a forward slash therefore normally it is not accepted however
-
01:44:36 normally it is not accepted however since we have added a asterisk character
-
01:44:40 since we have added a asterisk character now it should work
-
01:44:55 let's try okay it's working let's try older one
-
01:45:06 okay working as expected and let's add another slash
-
01:45:07 another slash yes so you see with a asterisk character
-
01:45:11 yes so you see with a asterisk character now everything that comes after this
-
01:45:14 now everything that comes after this slash
-
01:45:14 slash is considered as a c link not a
-
01:45:18 is considered as a c link not a special url so this is how you handle
-
01:45:22 special url so this is how you handle urls into the methods how you convert
-
01:45:24 urls into the methods how you convert them
-
01:45:25 them this is all about power of
-
01:45:29 this is all about power of routing
-
01:45:37 optional parameters in the examples above
-
01:45:38 above both the entry id and the slug
-
01:45:40 both the entry id and the slug parameters were required if omitted in
-
01:45:41 parameters were required if omitted in the url
-
01:45:42 the url the root template would simply not match
-
01:45:44 the root template would simply not match the requested url
-
01:45:46 the requested url and the result will be either a 404 page
-
01:45:48 and the result will be either a 404 page not found or it would be caught by the
-
01:45:50 not found or it would be caught by the default slash fallback route
-
01:45:53 default slash fallback route however sometimes you want to make one
-
01:45:55 however sometimes you want to make one or several parameters optional
-
01:45:56 or several parameters optional meaning that they can be omitted without
-
01:45:58 meaning that they can be omitted without the route failing to match the url
-
01:46:00 the route failing to match the url request
-
01:46:02 request for instance you may decide that the url
-
01:46:04 for instance you may decide that the url slug for your blog post
-
01:46:06 slug for your blog post should be optional after all the id is
-
01:46:08 should be optional after all the id is enough to identify the desired blog post
-
01:46:11 enough to identify the desired blog post and display it to the user
-
01:46:12 and display it to the user while the slug part is often included
-
01:46:14 while the slug part is often included for seo slash readability reasons
-
01:46:17 for seo slash readability reasons making the slug part of the url optional
-
01:46:19 making the slug part of the url optional is as simple as adding a question mark
-
01:46:21 is as simple as adding a question mark to the parameter name
-
01:46:28 root blog slash okay so in this case uh our tuples uh
-
01:46:32 uh our tuples uh will change
-
01:46:35 will change and
-
01:46:51 so uh let's also add that to our routing in our
-
01:46:59 tribute routine here i will add another word
-
01:47:01 word by the way it may also cause a problem
-
01:47:03 by the way it may also cause a problem with
-
01:47:05 with this one they may conflict but i am not
-
01:47:07 this one they may conflict but i am not sure okay
-
01:47:10 sure okay so we will test it if they will conflict
-
01:47:13 so we will test it if they will conflict or not i am not sure
-
01:47:21 we have made it both it can be everything anything
-
01:47:23 everything anything or it is optional
-
01:47:37 okay so yes working yes or again working okay so optional is
-
01:47:41 yes or again working okay so optional is also
-
01:47:43 also working
-
01:47:51 so we can also use this i believe let's try
-
01:47:59 okay it says article is not found probably it is because it is getting a
-
01:48:01 probably it is because it is getting a null as a
-
01:48:33 so this citizen ketu is being nulled probably i'm not sure
-
01:48:36 probably i'm not sure but let's test it
-
01:48:56 yeah it is not that bush [Music]
-
01:48:58 [Music] i have to change the teeth
-
01:49:05 huh simply [Music]
-
01:49:14 okay can we add this as a node it is working nice let's retry
-
01:49:53 okay working so this is uh both is working so they are not completing
-
01:49:55 is working so they are not completing because
-
01:49:56 because since the in the second
-
01:50:00 since the in the second way we don't have but if we add this
-
01:50:03 way we don't have but if we add this still working and let's see why
-
01:50:10 still working and let's see why so the keto is still being known okay so
-
01:50:13 so the keto is still being known okay so both is working
-
01:50:18 however if we even add a character here then it will become like this okay
-
01:50:20 then it will become like this okay as expected it is working all right
-
01:50:24 as expected it is working all right and let's continue if you do so
-
01:50:28 and let's continue if you do so the slug parameter will be null this is
-
01:50:31 the slug parameter will be null this is not a problem when using it for output
-
01:50:33 not a problem when using it for output the way we do
-
01:50:34 the way we do but if you tried using the value for
-
01:50:35 but if you tried using the value for other purposes it could result in a null
-
01:50:38 other purposes it could result in a null reference exception
-
01:50:40 reference exception to remedy this you should either check
-
01:50:41 to remedy this you should either check for null before using it or
-
01:50:43 for null before using it or if it makes more sense in your case add
-
01:50:45 if it makes more sense in your case add a default string value to the parameter
-
01:50:47 a default string value to the parameter empty or non-empty like this so it is an
-
01:50:50 empty or non-empty like this so it is an empty default setting
-
01:50:52 empty default setting and there is also the reversed routing
-
01:50:54 and there is also the reversed routing names is important
-
01:50:56 names is important reserved routing names when creating
-
01:50:59 reserved routing names when creating your roots and specifying for instance
-
01:51:01 your roots and specifying for instance parameter names you should pay attention
-
01:51:04 parameter names you should pay attention to the following words
-
01:51:05 to the following words which are reserved within the routing
-
01:51:06 which are reserved within the routing system and therefore shouldn't be used
-
01:51:08 system and therefore shouldn't be used as name slash parameter names
-
01:51:11 as name slash parameter names action area
-
01:51:15 action area controller handler
-
01:51:19 controller handler page so what does this mean this simply
-
01:51:21 page so what does this mean this simply means this let me show you
-
01:51:30 okay so when we define parameter names like here we can't use these special
-
01:51:34 like here we can't use these special keywords
-
01:51:35 keywords okay such as
-
01:51:40 okay such as action okay so if you define something
-
01:51:43 action okay so if you define something like this it will draw
-
01:51:45 like this it will draw an airbrush
-
01:51:48 an airbrush probably let's try if you want
-
01:51:57 okay by the way this and this would conflict so
-
01:52:02 conflict so let's change this article 2 and let's
-
01:52:16 try and or area control all of these are
-
01:52:19 and or area control all of these are prohibited
-
01:52:20 prohibited uh keywords okay
-
01:52:23 uh keywords okay oh it has already uh thrown an error
-
01:52:27 oh it has already uh thrown an error you see it cannot contain parameter
-
01:52:29 you see it cannot contain parameter action this is great this is why
-
01:52:32 action this is great this is why dotnet is great because
-
01:52:35 dotnet is great because you see you immediately see the errors
-
01:52:39 you see you immediately see the errors this is how it is strong typed
-
01:52:42 this is how it is strong typed programming language
-
01:52:44 programming language and whenever it is possible the
-
01:52:48 and whenever it is possible the net framework warns you about errors
-
01:52:51 net framework warns you about errors it didn't drop any error at the compile
-
01:52:54 it didn't drop any error at the compile time but immediately
-
01:52:55 time but immediately at the runtime we get an error and so we
-
01:52:59 at the runtime we get an error and so we can fix our error
-
01:53:00 can fix our error immediately okay
-
01:53:11 okay these words are not allowed as
-
01:53:14 are not allowed as parameter names such as below
-
01:53:43 summary summary as you can see url routing is very
-
01:53:46 as you can see url routing is very flexible
-
01:53:47 flexible when using the template functionality
-
01:53:48 when using the template functionality illustrated in this article
-
01:53:51 illustrated in this article however sometimes a root can become too
-
01:53:53 however sometimes a root can become too flexible meaning that it might conflict
-
01:53:55 flexible meaning that it might conflict with other roots in your website
-
01:53:57 with other roots in your website for these situations among others you
-
01:53:59 for these situations among others you need to take more control of your roots
-
01:54:01 need to take more control of your roots this can be achieved with routing
-
01:54:03 this can be achieved with routing constraints which we'll discuss in the
-
01:54:04 constraints which we'll discuss in the next article
-
01:54:06 next article okay so let's continue to
-
01:54:10 okay so let's continue to uh running constraints
-
01:54:14 uh running constraints it is the last part of running
-
01:54:27 routing constraints in the previous articles of this chapter
-
01:54:28 articles of this chapter we saw how we could create very flexible
-
01:54:31 we saw how we could create very flexible and thereby broad matching root
-
01:54:32 and thereby broad matching root templates
-
01:54:33 templates to catch many different kind of url
-
01:54:35 to catch many different kind of url structures and map them correctly
-
01:54:37 structures and map them correctly however at some point your roots may
-
01:54:39 however at some point your roots may become matched too broadly
-
01:54:41 become matched too broadly or perhaps you just have a very specific
-
01:54:43 or perhaps you just have a very specific structure that you want to enforce
-
01:54:46 structure that you want to enforce for this asp.net mvc has introduced the
-
01:54:49 for this asp.net mvc has introduced the clever concept of root constraints
-
01:54:51 clever concept of root constraints allowing you to be much more specific
-
01:54:53 allowing you to be much more specific when defining the parameters of your
-
01:54:55 when defining the parameters of your roots
-
01:54:56 roots to illustrate how routing constraints
-
01:54:58 to illustrate how routing constraints can help let's take a look at the
-
01:55:00 can help let's take a look at the example from the previous article
-
01:55:02 example from the previous article where we created a root for handling a
-
01:55:03 where we created a root for handling a classic id slash url slug scenario
-
01:55:07 classic id slash url slug scenario root blog slash entry id slash slug
-
01:55:11 root blog slash entry id slash slug public eye action result blog
-
01:55:17 there are several types of root constraints so let's use this example to
-
01:55:19 constraints so let's use this example to see how they all work
-
01:55:21 see how they all work data type constraints in this case
-
01:55:25 data type constraints in this case we use an integer for the entry id
-
01:55:27 we use an integer for the entry id parameter so we might as well enforce
-
01:55:29 parameter so we might as well enforce the directly in the template
-
01:55:31 the directly in the template using the special root constraint syntax
-
01:55:34 using the special root constraint syntax okay so with this uh constraint settings
-
01:55:38 okay so with this uh constraint settings will would not get catched by
-
01:55:42 would not get catched by this action so
-
01:55:45 this action so let's uh simply uh illustrate that
-
01:55:54 okay k is one and it will take string k one and two
-
01:55:55 one and two i will add a loading such as
-
01:56:00 i will add a loading such as okay uh cons
-
01:56:03 okay uh cons keyword key2 okay
-
01:56:06 keyword key2 okay and let's return
-
01:56:14 content okay spawn
-
01:56:28 and key two and now i will use the same
-
01:56:43 two and now i will use the same okay
-
01:56:50 this will be k2 however it will this time integer so it will be int
-
01:56:53 integer so it will be int and this will be k s two you see it is
-
01:56:57 and this will be k s two you see it is using almost exactly the same
-
01:57:00 almost exactly the same however the one one of them uh
-
01:57:03 however the one one of them uh constrained integers the other one
-
01:57:05 constrained integers the other one constrained
-
01:57:06 constrained uh no constraints so let's see
-
01:57:10 uh no constraints so let's see i'm not sure maybe we need to put here
-
01:57:12 i'm not sure maybe we need to put here string constraint
-
01:57:13 string constraint but let's see it
-
01:57:39 okay so this is case one and let's try this okay so okay it is working as
-
01:57:42 this okay so okay it is working as expected
-
01:57:43 expected you see when this is the integer it goes
-
01:57:45 you see when this is the integer it goes case two when it is not integer
-
01:57:47 case two when it is not integer okay such as this it goes to case well
-
01:57:52 okay such as this it goes to case well ok
-
01:57:58 the difference can be found in the where i have added and like this
-
01:58:00 i have added and like this entry id int now the url will only match
-
01:58:03 entry id int now the url will only match the route if the entry id parameter
-
01:58:05 the route if the entry id parameter is an integer like this slash blog slash
-
01:58:09 is an integer like this slash blog slash one fifty three slash testing the system
-
01:58:12 one fifty three slash testing the system slash
-
01:58:13 slash urls like this one will no longer match
-
01:58:17 urls like this one will no longer match slash blog slash abc 153 def
-
01:58:21 slash blog slash abc 153 def testing the system slash data type
-
01:58:24 testing the system slash data type constraint types
-
01:58:25 constraint types so which constraint type status and here
-
01:58:29 so which constraint type status and here the full list
-
01:58:31 the full list let's add to our page
-
01:58:48 so we can make boolean date time double flow take a moment for example
-
01:58:50 flow take a moment for example let's apply one of them okay such
-
01:58:53 let's apply one of them okay such as
-
01:59:02 uh long and integer may get problematic however i think
-
01:59:05 problematic however i think boolean should work
-
01:59:40 cons okay so let's try true yes now goes to take case three
-
01:59:45 true yes now goes to take case three okay or false
-
01:59:49 okay or false if we set it as zero okay
-
01:59:52 if we set it as zero okay it is working as expected
-
02:00:08 length slash range constraints another type of root constraints will
-
02:00:10 another type of root constraints will help you enforce a certain minimum
-
02:00:11 help you enforce a certain minimum and or maximum length for strings or a
-
02:00:14 and or maximum length for strings or a minimum slash maximum value for numeric
-
02:00:16 minimum slash maximum value for numeric types
-
02:00:17 types we could use that to make our root
-
02:00:19 we could use that to make our root template from before even more specific
-
02:00:22 template from before even more specific for instance since the entry id should
-
02:00:24 for instance since the entry id should likely match a database entry
-
02:00:26 likely match a database entry we could argue that the value should be
-
02:00:28 we could argue that the value should be higher than zero
-
02:00:29 higher than zero we can use the min constraint to do that
-
02:00:33 we can use the min constraint to do that root blog slash it okay so let's
-
02:00:36 root blog slash it okay so let's uh try this
-
02:00:41 uh try this okay i'm going to use
-
02:00:50 this time cons 2 okay and mid yeah
-
02:00:54 okay and mid yeah minimum one
-
02:01:13 one case five i one rectangle this will be
-
02:01:14 be inclusive or exclusive mean max
-
02:01:30 okay let's try so we have two different cases
-
02:01:31 cases minimum and maximum and according to
-
02:01:35 minimum and maximum and according to that
-
02:01:35 that [Music]
-
02:01:38 [Music] the case will change
-
02:01:51 the case will change okay
-
02:02:02 so this is case five if we said it mean the one okay so you see
-
02:02:05 the one okay so you see these are overlapping cases therefore
-
02:02:09 these are overlapping cases therefore it did output uh case four
-
02:02:17 if it said is zero this this k is five it we said it two
-
02:02:19 it we said it two it is case four
-
02:02:26 okay so if we have overlapping cases what happens if we change these i wonder
-
02:03:03 you comes okay this is going case one okay so you see the first
-
02:03:07 one okay so you see the first case that it enters uh
-
02:03:10 case that it enters uh working so the first case it enters is
-
02:03:15 working so the first case it enters is case two that is
-
02:03:18 case two that is when the constraint order comes into
-
02:03:23 when the constraint order comes into kicking
-
02:03:48 yeah we can also define routing orders and in this case
-
02:03:56 how do we define it order yeah so
-
02:03:59 order yeah so maybe here yes order
-
02:04:03 maybe here yes order one okay
-
02:04:09 so i will make this higher order okay order two
-
02:04:21 and order three order four so if there are multiple
-
02:04:24 order four so if there are multiple cases where it satisfies the conditions
-
02:04:28 cases where it satisfies the conditions you can define
-
02:04:29 you can define order and base it on that order it will
-
02:04:42 okay there are also other constraints such as
-
02:04:44 such as range min length
-
02:04:48 range min length minimum length maximum length
-
02:04:52 minimum length maximum length even regular expressions such as this
-
02:04:55 even regular expressions such as this one
-
02:04:57 one so if it matches it will get into that
-
02:05:06 however i wouldn't suggest you to use regular expression unless you
-
02:05:09 regular expression unless you need it extremely
-
02:05:13 need it extremely okay so let's now retest our order
-
02:05:22 cons one one okay case four you see this time it is getting into case four
-
02:05:25 this time it is getting into case four why
-
02:05:26 why uh because the case four has minimum one
-
02:05:29 uh because the case four has minimum one with order one
-
02:05:31 with order one it would also satisfy case 5
-
02:05:35 it would also satisfy case 5 but the case 5 has order 2
-
02:05:38 but the case 5 has order 2 therefore this is where it is captured
-
02:05:42 therefore this is where it is captured so when i set this as 2 it is case 4
-
02:05:47 so when i set this as 2 it is case 4 or 0 it is still case
-
02:05:50 or 0 it is still case 4 because i have to change this
-
02:05:53 4 because i have to change this now it is case 5. so are there any
-
02:05:57 now it is case 5. so are there any possibility that i can enter
-
02:05:59 possibility that i can enter k z1 or case 2 in this case
-
02:06:03 k z1 or case 2 in this case yes if i don't provide or
-
02:06:07 yes if i don't provide or no these are all socials so now i am i
-
02:06:09 no these are all socials so now i am i cannot
-
02:06:10 cannot enter inside
-
02:06:17 enter inside case two but i can enter inside case
-
02:06:20 case two but i can enter inside case one uh how if i change this to a
-
02:06:24 one uh how if i change this to a non-integer it can work like this
-
02:06:28 non-integer it can work like this okay let's also type the other
-
02:06:30 okay let's also type the other constraints
-
02:06:33 constraints okay so
-
02:06:36 okay so i will just add them as a node because
-
02:06:38 i will just add them as a node because it is just
-
02:06:39 it is just basically same nothing is different
-
02:06:50 okay we have writing that so there is range
-
02:07:02 uh you can also combine multiple constraints
-
02:07:03 constraints such as this one this one
-
02:07:07 such as this one this one combines range
-
02:07:10 combines range and min and max length like this here is
-
02:07:13 and min and max length like this here is the combination
-
02:07:19 this is the combination of integer range both intention and range
-
02:07:27 okay you can also have regular extension
-
02:07:30 you can also have regular extension constraints
-
02:07:33 constraints such as
-
02:07:40 this one okay so let's add this page and i wonder if
-
02:07:43 so let's add this page and i wonder if there are any other constraints
-
02:07:45 there are any other constraints let's see
-
02:08:08 2013 okay
-
02:08:14 so this is a regular expression constraint i would say
-
02:08:23 constraint i would say maybe some more decent
-
02:08:26 maybe some more decent page this recently reading page
-
02:08:37 page this recently reading page so this is optional id parameter
-
02:08:45 id integers alpha interesting what is alpha
-
02:09:08 okay there is also root areas root area is there nothing but way to specify the
-
02:09:10 is there nothing but way to specify the area in the root basically just to let
-
02:09:12 area in the root basically just to let you the road know
-
02:09:14 you the road know that the control goes to some area
-
02:09:16 that the control goes to some area rotary
-
02:09:25 okay oh alpha is this one which is uppercase or lowercase is
-
02:09:27 which is uppercase or lowercase is nothing okay so the attributes are
-
02:09:30 nothing okay so the attributes are fully written here and
-
02:09:33 fully written here and let's write down here
-
02:09:37 let's write down here okay i think it's a good source
-
02:09:45 so you can use any of these constraints like
-
02:09:47 like this okay so there's alpha bull date
-
02:09:51 this okay so there's alpha bull date time
-
02:09:52 time uh decimal double float
-
02:09:55 uh decimal double float y d integer length long max max like me
-
02:10:01 y d integer length long max max like me milling range regex okay
-
02:10:09 okay so uh for today it is enough and i am sorry that i was a bit sick
-
02:10:12 i am sorry that i was a bit sick therefore
-
02:10:12 therefore um by i have a running dose
-
02:10:18 um by i have a running dose therefore i have to i had to pause the
-
02:10:20 therefore i have to i had to pause the video a lot
-
02:10:22 video a lot uh during the lectures but i didn't want
-
02:10:24 uh during the lectures but i didn't want to
-
02:10:25 to delay back so let's
-
02:10:29 delay back so let's take our context node as well
-
02:10:32 take our context node as well as well
-
02:10:43 okay in lecture six what we have done and
-
02:10:52 how to compose an empty from scratch uh it's not net
-
02:10:56 scratch uh it's not net course and we say that application
-
02:11:01 course and we say that application without
-
02:11:07 any templates okay uh how to convert
-
02:11:10 uh how to convert an empty asp.net course
-
02:11:14 an empty asp.net course web application into
-
02:11:18 web application into mvc application
-
02:11:23 mvc application okay how did you convert it we have
-
02:11:25 okay how did you convert it we have ended
-
02:11:27 ended uh startup file
-
02:11:30 uh startup file services at mvc
-
02:11:36 how to okay how to support both custom
-
02:11:40 support both custom routing and defaults
-
02:11:54 loading in and that's cause
-
02:12:06 application what does okay
-
02:12:35 it also like this as well so let's use the titles here
-
02:12:42 leonardo when we see road road templates
-
02:12:48 road templates okay
-
02:13:00 to the same co same h same old shoulders
-
02:13:04 same old shoulders okay
-
02:13:58 i think okay routing templates
-
02:14:24 okay reverse its routing names is also important
-
02:14:25 important if you use them you will get a runtime
-
02:14:28 if you use them you will get a runtime error
-
02:14:54 that's the type of shades okay i think it is enough
-
02:14:57 okay i think it is enough so let's upload our
-
02:15:57 okay it is upload let's hopefully see you next week please
-
02:15:59 you next week please be careful of coronavirus it is really
-
02:16:02 be careful of coronavirus it is really serious
-
02:16:04 serious and hopefully see you next week
