Part 28. _Viewstart.cshtml in NET Core MVC | Conditional Layout Pages. | AspNetCoreMVC.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome to programming concepts my name is amit and this is part 28 of asp.net code mvc tutorial in this video we will talk about what is viewstart.cshtml indo.net core msc this is continuation to part 27 layout view in dot net core mpc so please watch it before proceeding to this one i shared the link in the description let's get back to visual studio and this is the project which we are working on let's run the program first as discussed in our last video we observed the importance of repetitive layout in live websites and to implement the same in our project we created a layout page so here this header links on the right side and footer are rendered with the help of layout dot cs html page let's get back to visual studio and here is our layout page if i go to index dot cs html page you can see here is the small code which we write to inform our compiler that we want to use this layout page dot against html now if i create another page say delete employee.cs html then let's create that so let's go to employer controller right click on delete employee add view and then we have to select razer empty click on add and we can name it delete employee dot cs html then click on it it will generate view for us let's delete the auto generated commented code and add h1 element and say message from delete implode and let's get back to controller and for now delete the parameter id we don't need it right now let's return view and change the return type from string to view result let's run the program once again let's navigate to employee slash delete employee and you can see it is only displaying the h1 tag and not the layout content can you guess why it's simply because we haven't added the reference to our layout page let's quickly add the reference so curly braces layout is equal to underscore layout semicolon so if we run it will give an error because in our previous video we made the script section mandatory let's make it optional so with the layout page set required to false and let's save the file and refresh the file and you can see we are getting required output now you can see the repetitive code in both the view in both views we write the code to reference the layout page and most of the time most of our pages share a common layout page so is there any way to reduce this repetition as well and the answer is yes with the help of view start.cs html page we can achieve that as the name suggests at this start if we want to set any properties for view we can use view start.csstml this is a special view which gets executed before other views let's see that in action so we place view start file at the top level within view folder so right click on view add new item and then search for razer view start and no need to change the name click on add you can see it automatically added the available layout page let's delete the layout reference from both the pages let's change it from delete employee page first and then remove it from index view save the page and let's run the program you can see it is working for index view let's navigate to delete employee view and you can see it is working for delete employee view as well now what will happen if we change the name from underscore view start dot cs html to let's say double underscore view start dot cs html let's see that stop the application change the name add underscore save the file and let's run the program once again you can see it is not working not referencing the layout page all right so the next statement from microsoft is we can create more than one view start file and they are heretical in nature so what does hierarchical mean here so first let's stop the application and then right click on the employee folder add new item search for razer view start and then click on it no need to change the name and let's correct the name of root view start file as well next we required multiple layout page for demo purpose similarly like view start page in the application we can also have multiple layout pages let's create another layout page just copy and paste the existing layout page and change the name to underscore layout to dot cs html open this file and just change the text from pro concept to layout tool just to differentiate between the output and in newly created viewstart.csstml page within employee folder let's dot reference html instead of underscore layout page so can you guess which layout will be applied let me run the program while you can guess the answer you can see it is giving preference to view start file which is closer to its own application the one present under the employee folder so whatever common property we will define preference will be given to the one which is closer to the view all right next what if we required some other layout for a specific view which we don't want to define on the view start page then we have an option to set the layout property with an individual view as well let's see that so set the layout property in index view add curly braces set layout is equal to underscore layer let's save the file and refresh the file you can see the output is as expected preference given to setting which is closer to its own location this time we specified layer property within view itself which is the closest point hence preference given to layout now what if we don't want to use any layout page then we need to simply set the property to null let's do that let's change it to null save the page and refresh the page you can see no layout page is applied to our index view next we can also set the conditional layout view within view start dot cs html page let's see that before that let's quickly delete the view start file with an employee folder we don't need it for now and delete layout property from our index view as well and let's go to view start page and implement conditional layout code let's use razer syntax to implement conditional layout page to our view and to use razer we need to use the symbol can you guess yes it is at so add curly braces then we need if condition so if let's say context dot request dot path dot tostring dot contains let's say delete employee here we are trying to check if url contains delete employee then we will apply some layout page it is for simplicity in general it is on permission basis like separate layout for managers hr finance department and other employers we haven't implemented any permission yet so i am using url for demo purpose so within curly braces layout is equal to underscore layout so if we go to delete employee then we are looking for underscore layout page else set layout is equal to underscore layout two so this if statement is self explanatory let's save the file and let's run the program you can see layout two pages for index.css html and let's navigate to employee slash delete employee and you can see it is using underscore layout.cs html alright so that's it in this video if you have any queries related to the content of this video do ask me in comments till then thanks for watching [Music]
Info
Channel: PRO Concepts
Views: 730
Rating: undefined out of 5
Keywords: asp.net core mvc, viewstart, example, explained, viewstart asp.net core, asp.net mvc choose layout, asp.net core select layout page, asp.net core viewstart example, asp.net mvc multiple layout pages, proconcepts, aspnetcoremvc, aspnetcore without entity framework, amit, amit singh rawat, asp.net core without entity framework, asp.net core mvc playlist, viewstart.cshtml, _viewstart.cshtml, viewstart.cshtml in .net core mvc, Use of Viewstart page in .net core mvc, viewstart page usage
Id: 5bf9k1M2s1w
Channel Id: undefined
Length: 11min 53sec (713 seconds)
Published: Sun Mar 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.