Please enable JavaScript.
Coggle requires JavaScript to display documents.
ASP.NET Core (Razor Pages
Is a new aspect of ASP.NET Core MVC that makes…
ASP.NET Core
Razor Pages
Is a new aspect of ASP.NET Core MVC that makes coding page-focused scenarios easier and more productive
Directive
-
It handles requests directly, without going through a controller
-
Custom routes
-
-
Append parameters to a page's default route. For example, an ID parameter, id
, can be required for a page with @page "{id}"
You don't like the query string ?handler=JoinList
. You can customize the route by adding a route template with @page "{handler?}"
The ?
following handler
means the route parameter is optional
-
Model binding, Tag Helpers, and HTML helpers all just work with the properties defined in a Razor Page class #
-
-
-
Filter methods
- Run code after a handler method has been selected, but before model binding occurs.
- Run code before the handler method executes, after model binding is complete.
- Run code after the handler method executes.
- Can be implemented on a page or globally.
- Cannot be applied to specific page handler methods.
Can be used to add log, authorize logic and so on
MVC
Model
-
-
API
Expose a data to client maybe different from domain data. It helps separete scopes, and cause less impact to clients consuming the API data types. You can change your under layer safelly.
Persistence
Maybe useful to separate domain data in different data models, which encapsulate specific business logic
Domain
Encapsulate complex business logic, making easy to uint tests. Expose a way to hande states by the view, controller and even services.
View
Present content through the user interface. Use Razor to embed .NET code in HTML. Consider use View Component to handle complex logic
-
Features
-
Model binding
Order of binding
- Form value
- Route value
- Query value
All stored as name-value paris
-
Special cases
IFormFile,
IEnumerable<IFormaFile>,
CacelationToken
-
-
-
-
-
-
Views
Tag Helpers
Enable server-side code to participate in creating and rendering HTML elements in Razor files
Why?
- IntelliSense support for Tag Helpers
- More productive
- Produce more robust, reliable, and maintainable code using information only available on the server
Directives
-
-
-
tagHelperPrefix - to make Tag Helper usage explicit
Tag Helper will be available just in elements with the prefix
Opting out - disable Tag Helper at the element level
<!span asp-validation-for="Email" class="text-danger"></!span>
-
-
View components
are similar to partial views in that they allow you to reduce repetitive code, but they're appropriate for view content that requires code to run on the server in order to render the webpage
Also work with Razor Pages #
Why?
- Renders a chunk rather than a whole response
- Includes the same sepration-of-concerns and testability benefits found between a controller and a view
- Can have parameters and business logic
- Is typically invoked from a layout page
Does not take part in the controller lifecycle, which means you can't use filters in the view component
View search path
- /Pages/Components//<view_name>
- Views/<controller_name>/Components/<view_component_name>/<view_name>
- Views/Shared/Components/<view_component_name>/<view_name>
-
-
-
View
View discovery
- Views/[ControllerName]/[ViewName].cshtml
- Views/Shared/[ViewName].cshtml
Passing data to views
Strongly typed data: viewmodel
Weakly typed data:
- ViewData
- ViewBag (not available in Razor Pages) #
Why?
- Simplify the controller
- Override previously injected services on page
-