BMIT2023 Chapter 02
BMIT2023 Chapter 02
202409
BMIT2023 Web and Mobile Systems Slide 2
Part 1:
Reusable Layout
202409
BMIT2023 Web and Mobile Systems Slide 3
Reusable Layout
Most web apps have a common layout.
Provide users with consistent experience as they
navigate from page to page.
Layout typically includes common UI elements.
For example: Header
Header Navigation Menu
Navigation Menu Page Content
Footer
Footer
202409
BMIT2023 Web and Mobile Systems Slide 4
Reusable Layout …
Common HTML structures such as shared JS and CSS
imports are also included in layout.
Layout reduces duplicate code in views.
NOTE:
Layout is optional.
Apps can define more than one layout.
Different views may use different layouts.
By convention, the default layout for ASP.NET is named
_Layout.cshtml and stored in the Views/Shared folder.
202409
BMIT2023 Web and Mobile Systems Slide 5
202409
BMIT2023 Web and Mobile Systems Slide 6
…
Drag-and-drop or copy-and-paste an PNG app icon to the images folder.
Rename it to favicon.png if necessary.
202409
BMIT2023 Web and Mobile Systems Slide 7
…
Right-click css folder. Enter app.css.
Write common
CSS code here
202409
BMIT2023 Web and Mobile Systems Slide 8
…
Right-click js folder. Enter app.js.
Write common
JS code here
202409
BMIT2023 Web and Mobile Systems Slide 9
202409
BMIT2023 Web and Mobile Systems Slide 10
202409
BMIT2023 Web and Mobile Systems Slide 11
…
The JS library is downloaded from CDNJS.
The libman.json file is also created.
NOTE:
We can continue to add JS libraries using the UI.
Or by modifying the libman.json file.
202409
BMIT2023 Web and Mobile Systems Slide 12
…
Open libman.json file. Modify:
202409
BMIT2023 Web and Mobile Systems Slide 13
…
Add the other libraries.
Save the file.
The libraries will be added.
202409
BMIT2023 Web and Mobile Systems Slide 14
…
NOTE: We can click the light bulb icon to uninstall or check for updates.
202409
BMIT2023 Web and Mobile Systems Slide 15
202409
BMIT2023 Web and Mobile Systems Slide 16
…
Right-click Shared folder. Select Add > View.
Select Razor View – Empty.
202409
BMIT2023 Web and Mobile Systems Slide 17
…
Enter the name _Layout.cshtml can ignore the file extension.
202409
BMIT2023 Web and Mobile Systems Slide 18
…
We can then write the code for layout here.
202409
BMIT2023 Web and Mobile Systems Slide 19
…
Code for layout:
202409
BMIT2023 Web and Mobile Systems Slide 20
…
Continue:
202409
BMIT2023 Web and Mobile Systems Slide 21
…
NOTE: The JS files must be imported in the following sequence:
Depends on…
jQuery AJAX jQuery Validate
Unobtrusive
Depends on…
jQuery Validation
Unobtrusive
202409
BMIT2023 Web and Mobile Systems Slide 22
Name Required
202409
BMIT2023 Web and Mobile Systems Slide 23
202409
BMIT2023 Web and Mobile Systems Slide 24
202409
BMIT2023 Web and Mobile Systems Slide 25
…
Right-click Views folder. Select Add > View.
Select Razor View – Empty.
Enter file name _ViewStart.cs.
Modify the code:
202409
BMIT2023 Web and Mobile Systems Slide 26
…
For individual views:
202409
BMIT2023 Web and Mobile Systems Slide 27
…
Run the project. Result:
202409
BMIT2023 Web and Mobile Systems Slide 28
202409
BMIT2023 Web and Mobile Systems Slide 29
Views in Admin
folder use this layout
202409
BMIT2023 Web and Mobile Systems Slide 30
_ViewStart.cshtml is Hierarchical
Recall that _ViewStart.cshtml file can be used to:
Load default layout
Provide default values to variables ViewBag
Execute common view startup logic
And _ViewStart.cshtml is hierarchical.
We can also add separated _ViewStart.cshtml file for
each individual view folder.
The outer one will run first. The inner one will run next
and override any duplicated settings.
Finally, the individual view is executed.
202409
BMIT2023 Web and Mobile Systems Slide 31
_ViewStart.cshtml is Hierarchical …
Example: Assume that we visit /Home/Index…
202409
BMIT2023 Web and Mobile Systems Slide 32
Tag Helpers
Tag helpers extend the functionality of HTML elements
by supporting non-standard custom HTML attributes.
Enable server-side code to participate in creating and
rendering HTML elements in views.
Tag helpers are processed at server-side and rendered
into standard HTML elements and attributes which
browsers can understand.
202409
BMIT2023 Web and Mobile Systems Slide 33
Tag Helpers …
Example:
Import built-in tag helpers to a view:
202409
BMIT2023 Web and Mobile Systems Slide 34
Tag Helpers …
NOTE:
Not all HTML elements are supported by tag helpers.
Built-in tag helper attributes always start with "asp".
VS2022 auto code completion will suggest the supported tag
helper attributes if you start an attribute with "asp".
202409
BMIT2023 Web and Mobile Systems Slide 35
202409
BMIT2023 Web and Mobile Systems Slide 36
…
Open _Layout.cshtml.
Modify the <link> element that imports app.css:
NOTE:
This tag helper attribute append a version string (hash) to the end of the URL.
If the file content changes, the version string changes.
This prevent browsers from loading outdated file from cache.
This tag helper attribute also works on <img> element.
202409
BMIT2023 Web and Mobile Systems Slide 37
…
Run the project.
On browser, right-click and select View Page Source.
202409
BMIT2023 Web and Mobile Systems Slide 38
Standard HTML
Tag Helper
HTML Helper
202409
BMIT2023 Web and Mobile Systems Slide 39
** HTML helpers are still used for scenarios that cannot be handled by tag helpers
202409
BMIT2023 Web and Mobile Systems Slide 40
Part 2:
Partial View
202409
BMIT2023 Web and Mobile Systems Slide 41
Partial View
Partial view is a reusable UI component used to render a
portion of a web page.
Commonly used to break down large views in smaller,
manageable and reusable parts.
Helps to avoid redundant codes by rendering shared
content in multiple places across an application.
Partial view is usually used together with AJAX
programming to support partial page reload.
NOTE: We will cover AJAX
programming in future
NOTE: A partial view
works like PHP include
202409
BMIT2023 Web and Mobile Systems Slide 42
Partial View …
NOTE:
File name of partial view begins with an underscore (_) by
convention. For example: _Hello.cshtml.
Can be saved in the Shared folder (globally accessible) or the
specific controller view folder (accessible within the folder).
202409
BMIT2023 Web and Mobile Systems Slide 43
202409
BMIT2023 Web and Mobile Systems Slide 44
…
Modify the code of _Photo.cshtml:
CSS to style the elements.
Will be rendered together
202409
BMIT2023 Web and Mobile Systems Slide 45
…
Modify the code of Home/Page1.cshtml:
202409
BMIT2023 Web and Mobile Systems Slide 46
…
Result:
202409
BMIT2023 Web and Mobile Systems Slide 47
NOTE:
Partial view is a fast and easy approach to create reusable UI
component. However, it is not backed by a C# class, hence
unable support complex backend logic.
View Component is a more powerful way to create reusable UI
component with complex backend logic. We do not cover View
Component. Find out more from the following reference:
https://learn.microsoft.com/aspnet/core/mvc/views/view-components
202409
BMIT2023 Web and Mobile Systems Slide 48
202409
BMIT2023 Web and Mobile Systems Slide 49
…
Modify the code of _RepeatText.cshtml:
Access to Count
Access to Text
202409
BMIT2023 Web and Mobile Systems Slide 50
…
Modify the code of Home/Index.cshtml:
NOTE: Single quote is used here Value for Text Value for Count
so that it does not conflict with
the double quote for string
202409
BMIT2023 Web and Mobile Systems Slide 51
…
Result:
202409
BMIT2023 Web and Mobile Systems Slide 52
Self-Check Questions
1. What are the advantages of using reusable layout in ASP.NET
MVC web applications?
202409
BMIT2023 Web and Mobile Systems Slide 53
THE END!!!
202409