hello friends welcome to weapon technologies today we are going to discuss about attribute routing in mvc so uh let's start and let me tell you one more thing if you are very new to this routing concept then i will strongly recommend you to watch my last previous video on routing and conventional routing so that you can become familiar with all these keywords which is i'm going to use in this video right so uh let's start attribute routing for using attribute routing in a vc application we need to uh tell the application that we are going to use attribute routing right it is very simple and very easy to use attribute routing in any mvc application in comparison to the conventional routing right so let's go to the application and i am continuing with the same example that we have used in the previous video for the conventional routing right so you can see here the same example that which we have discussed in the last video right so uh while using attribute routing we need to tell the application that we are going to use attribute route right so uh let me comment all this thing because now we are not going to use conventional routing right so let me comment it first so i have commented here right and now we need to tell the application that we are going to use that's it we need to write only this one line routes dot map mvc attribute routes by defining this one line that application will come to know that we are going to use attribute routing instead of conventional routing right and one more thing that while using conventional routing we need to define all these parameters we can see all these parameters in the dot config file right but in the case of attribute routing that we will deal with the controller and action level we will not use uh any uh we will not use all these parameters or attributes in the config file we will simply deal with the on the controller and the action level that's it right so let's start so already we uh i have commented so we are not going to use uh the conventional approach and we have already tell the application that we are going to use attribute routing this line also we can define in the global.acx file in this file right so uh here also we can define or either we can define here also so anywhere the place which you want you can define this line so let's save the changes and now let's go to the controller and over here we need to define the attributes on the top of the action level right we can define so let's define for the first route and over here we need to define the pattern remember one thing the concept is almost same uh if you see when we were using conventional routing we need to define this url this is basically a pattern right which we know which we need to define the pattern right so the same we need to define the pattern over here so let me copy this and let me paste it over here now we have decorate this action method with this attribute right and we are telling that we have defined this pattern for this action method now we don't need to define any controller name or any action because it is already uh inside this controller and we are applying for this particular action method right so that's very simple to use that's it right now uh let me write for the remaining two action method also for this action method it is getting one record for given id right so we need to define the pattern accordingly so let's copy the pattern over here the same thing that what we have defined in this okay so let me delete this and we have defined over here the pattern for this action method also right and the same we will define for this action method and for this on the basis of id we need to fetch the address of a particular employee right so over here we will write address if you see in the la in the conventional routing you can see for this we design the pattern like this right so the same pattern we are defining on the top of the action method right so that's it i am saving the changes and now run the application the application is loading so let's wait for a couple of seconds and accordingly we need to define the without in the url so we need to define the url right so employ and press enter yes now we can see the output it is calling the uh if we go into the code it is calling this get all empire details and it is giving the output accordingly and it is giving all the records of our employee and that we are expecting on the browser same right now now we want to get employee record on the basis of id so let's let pass the id in the url so passing the id let's suppose three oh we are getting another pie it is oh okay and why we are getting this error okay so let's go back to the application and check that out uh what we have defined okay uh there is a space you can see there is a space so we need to remove the space so let me stop the application first and let's remove the space in the pattern what we have defined in the without attribute we can see and the same we need to remove the space from here also right so the space space should not be there so keep this thing in mind and now save the changes and now i'm running the application again and let's run the application and see the output yes so now we need to pass in the url employee so let's see the output it will give the all employer the codes yeah we can see all the employer records and now we are we need to pass id parameter so on the basis of id also we are getting the output right and now let's uh go to the code and now we want to get the address of a particular employee on the basis of id so we need to pass address in the url that we have that we have already defined in the pattern right so let's go back to the browser and let's define the address so it will give the address of this particular employer we can see the output right so it is working fine right so you can see how simple and how easy it is it is to use right we need to define only the attributes on the top of the action method right and accordingly we can get the desired output and from where we are getting the data we have already defined this private method and we have already created a dummy data as we have discussed in the last video when we were working with the conventional routing right so let me stop the application so this is how your attribute routing works right and now one more thing we can see that uh on the top of the every action method this employee this word is common right so we can remove the common part from here and we can define it on the top of the controller right so the common part we can remove from here and we can define at the top of the controller and we can delete this uh from every action method so let's do it so let's copy it and for defining uh the prefix basically we see this prefix we need to write without prefix right and the common part employs a common part in every pattern we can see so we have defined on the top of the controller now it will be applicable to all the action method which is available inside this controller employer controller right so remove this common part remove this common part and now remove this common part right save the changes we need to remove this this slash also yeah now the common part is for the move in all the action methods right now let's see if the changes and now run the application now let's see whether it's working fine or not so let's pass again first of all employ it will display the all the records of all the employees we can see yeah we can see on the screen yes it is working fine now let's pass any id it will give the record on the basis of id yes and now we need to fetch the address of this employee we need to pass address in the url yes we are getting the desired output right so our attribute routing is working fine when we have defined this without prefix on the top of the controller so this is how we can uh define your attribute routing and all the common part right so let me stop the application and let me tell you one more thing now let me define one more action method right so what i have done i have simply created one public method which is expecting string id parameter right and it is returning the same id parameter but uh this action method is decorated with this with this uh pattern right and if you see for this action method and for this action method your pattern is same right so the route is same for two different action method so will it work if you remember when we discussed uh with conventional routing we have already discussed that that a same route cannot be defined for two different action methods right so and we are doing the same thing we are we are defining the same route we can see the same route for these two different action method will it work let's see into the picture save the changes and run the application ideally it will not work and it will not work so let's wait and let pass id right pressing the enter this time application will throw away because because application got confused which action method it has to call we can see it is uh it is saying the current request is ambiguous between the following action method right so application got confused application got confused which action method it has to call whether this action method get employed or this one get string data because your without is same for these two action method and this is not possible so that's why application got confused which action it has to call so that's why we need to define unique pattern for these two different action method right so let me stop the application so that's why it is throwing a error right so let me stop the application first and we need to define we need to tell the application we need to define the pattern right so over here we can define uh we can see we can apply constraints right we need to define that it is going to expect integer parameter the same that uh when we were discussing when we were discussing about constraint in conventional routing we can see we have uh applied the constraint right so accordingly we can apply constraints over here in the attribute routing so now this action method will be expecting this pattern right so it will accept those requests only which will be having integer parameter in the request right so for the integer parameter the controller the control will come here and and for any string parameter you know very well in the dotnet each and everything is uh tweeted in a string so for every request uh you can see uh if a parameter is not in teaser then the request will come over here so let's let's see if the changes and run the application to see into the picture whether it is working fine or not so application has been built successfully so let's go back here and now let's press enter with the same yeah we can see our debugger is coming to this integer one right and in the id it is giving five so it will give the output accordingly so the output will be displayed on the screen let's wait yeah we can see output we are getting and now let's suppose we are passing any string right and now pressing the enter it will go to the this action method which is expecting is which is going to expect string parameter and will return the that particular id yeah we can see so this is how basically it works so by this concept um this is also proved that a same route cannot be defined for two different action method a same route can cannot be defined for two different action method right but vice versa is possible right that that a single action method can have multiple multiple routes defined right so let's suppose let me create one more action method to make you understand right and i have defined a load for this action method over here we have defined let's suppose we name it contact us right and we are going to define over here contact us right now save the changes and i'm defining one more action method and in this we can see i have defined one action method with contact test which is going to return string right and for this we have defined the pattern we have defined the root for this action method is contact us right so let's see if the changes and run the application yeah but let's to find one more doubt because we are going to discuss that a single action method can have multiple routes and this time let's pass this route right now these two routes are different right and now save the changes run the application now we will see that both of these routes are going to work for this action method or not so let wait and let's pass the control the controller name this is what we have to find on the top of this controller we can see in the allowed prefix employee right and and after that we need to call for this action method so we need we need to define this right so let's go back to the browser and now we need to call contact us contact us pressing the enter yeah we can see we are getting the output this is a contact us page the same right and now let's call another route which we have to find for this action method this time also we are getting the output same output right so we have seen uh we have already seen that that for a single action method a multiple routes can be defined it is possible but a same route cannot be defined for two different action method that we have already seen in the picture right so this is how basically your attribute routing works and one more thing let me tell you one more thing that let's suppose we want to override this uh this prefix we want to override in for the given action method for that also we can do we need to apply that tilt symbol over here so let's suppose we we want to override this we need to apply tail symbol and then this now by doing this what we are doing that now in the url we we don't need to pass the full the full pattern that we need to pass employ then we need to pass the this pattern contact us but now this time we are overriding this without prefix this without prefix we are overriding for this given action method so for calling this action method on the browser we need to simply call contact us and it will work expecting it will work fine it will work as expected right so let's see into the picture saving the changes because we have already because we have override this action method by this tilt symbol for that without prefix right so let's see into the picture and now this time instead of passing with the employee we can simply call contact us contact us page right we can see this is a contact us page right when i'm pressing enter over here that is giving the output the same this is the contact space because we have override your without prefix for this given action method right so this is how your attribute routing works and uh this is all about the attribute routing right [Music] so uh that is for the attribute writing uh that's it for the attribute routing and please like and subscribe my channel fans and so that you can get all the latest notification in the future and please share with your friends right so that's it bye bye take care
2022-02-07