mirror of
https://github.com/PacktPublishing/Web-Development-with-Blazor-Second-Edition.git
synced 2026-04-21 06:03:59 +00:00
Initial commit
This commit is contained in:
parent
2190113c56
commit
3088165398
1765 changed files with 192085 additions and 0 deletions
22
Chapter05/MyBlog/Components/Components.csproj
Normal file
22
Chapter05/MyBlog/Components/Components.csproj
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<SupportedPlatform Include="browser" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Data.Models\Data.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
11
Chapter05/MyBlog/Components/Pages/AlertTest.razor
Normal file
11
Chapter05/MyBlog/Components/Pages/AlertTest.razor
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
@page "/alerttest"
|
||||
@using Components.RazorComponents
|
||||
<Alert Style="Alert.AlertStyle.Danger">
|
||||
This is a test
|
||||
</Alert>
|
||||
<Alert Style="Alert.AlertStyle.Success">
|
||||
<ChildContent>
|
||||
This is another test
|
||||
</ChildContent>
|
||||
</Alert>
|
||||
<Alert Style="Alert.AlertStyle.Success"/>
|
||||
18
Chapter05/MyBlog/Components/Pages/Counter.razor
Normal file
18
Chapter05/MyBlog/Components/Pages/Counter.razor
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
@page "/counter"
|
||||
|
||||
<PageTitle>Counter</PageTitle>
|
||||
|
||||
<h1>Counter</h1>
|
||||
|
||||
<p role="status">Current count: @currentCount</p>
|
||||
|
||||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
|
||||
|
||||
@code {
|
||||
private int currentCount = 0;
|
||||
|
||||
private void IncrementCount()
|
||||
{
|
||||
currentCount++;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
@page "/counterwithparameter"
|
||||
<h1>Counter</h1>
|
||||
<p>Current count: @CurrentCount</p>
|
||||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
|
||||
@code {
|
||||
[Parameter]
|
||||
public int IncrementAmount { get; set; } = 1;
|
||||
[Parameter]
|
||||
public int CurrentCount { get; set; } = 0;
|
||||
//private void IncrementCount()
|
||||
//{
|
||||
// CurrentCount+=IncrementAmount;
|
||||
//}
|
||||
|
||||
[Parameter]
|
||||
public EventCallback<int> CurrentCountChanged { get; set; }
|
||||
private void IncrementCount()
|
||||
{
|
||||
CurrentCount += IncrementAmount;
|
||||
CurrentCountChanged.InvokeAsync(CurrentCount);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<h3>ComponentWithError</h3>
|
||||
|
||||
|
||||
<button @onclick="@(()=>{ throw new Exception("This is an error");})">Throw error</button>
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
@page "/customerrorboundary"
|
||||
<ErrorBoundary>
|
||||
<ChildContent>
|
||||
<ComponentWithError />
|
||||
</ChildContent>
|
||||
<ErrorContent>
|
||||
<h1 style="color: red;">Oops... something broke</h1>
|
||||
</ErrorContent>
|
||||
</ErrorBoundary>
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
@page "/errorboundary"
|
||||
<ErrorBoundary>
|
||||
<ComponentWithError />
|
||||
</ErrorBoundary>
|
||||
13
Chapter05/MyBlog/Components/Pages/ErrorBoundaryTest.razor
Normal file
13
Chapter05/MyBlog/Components/Pages/ErrorBoundaryTest.razor
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
@page "/errorboundarytest"
|
||||
<ErrorBoundary Context=ex>
|
||||
<ChildContent>
|
||||
<p>@(1/zero)</p>
|
||||
</ChildContent>
|
||||
<ErrorContent>
|
||||
An error occured
|
||||
@ex.Message
|
||||
</ErrorContent>
|
||||
</ErrorBoundary>
|
||||
@code {
|
||||
int zero = 0;
|
||||
}
|
||||
30
Chapter05/MyBlog/Components/Pages/Index.razor
Normal file
30
Chapter05/MyBlog/Components/Pages/Index.razor
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
@page "/"
|
||||
@using Data.Models.Interfaces
|
||||
@using Data.Models
|
||||
@inject IBlogApi _api
|
||||
|
||||
<ul>
|
||||
<Virtualize ItemsProvider="LoadPosts" Context="p">
|
||||
<li><a href="/Post/@p.Id">@p.Title</a></li>
|
||||
</Virtualize>
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
@code {
|
||||
|
||||
public int totalBlogposts { get; set; }
|
||||
private async ValueTask<ItemsProviderResult<BlogPost>> LoadPosts(ItemsProviderRequest request)
|
||||
{
|
||||
if (totalBlogposts == 0)
|
||||
{
|
||||
totalBlogposts = await _api.GetBlogPostCountAsync();
|
||||
}
|
||||
var numblogposts = Math.Min(request.Count, totalBlogposts - request.StartIndex);
|
||||
var blogposts = await _api.GetBlogPostsAsync(numblogposts, request.StartIndex);
|
||||
return new ItemsProviderResult<BlogPost>(blogposts, totalBlogposts);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
10
Chapter05/MyBlog/Components/Pages/ParentCounter.razor
Normal file
10
Chapter05/MyBlog/Components/Pages/ParentCounter.razor
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
@page "/parentcounter"
|
||||
<CounterWithParameterAndEvent IncrementAmount="@incrementamount" @bind-CurrentCount="currentcount"/>
|
||||
The current count is: @currentcount
|
||||
@code {
|
||||
int incrementamount = 10;
|
||||
int currentcount = 0;
|
||||
|
||||
|
||||
|
||||
}
|
||||
30
Chapter05/MyBlog/Components/Pages/Post.razor
Normal file
30
Chapter05/MyBlog/Components/Pages/Post.razor
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
@page "/post/{BlogPostId}"
|
||||
@inject IBlogApi _api
|
||||
@inject NavigationManager _navman
|
||||
@if (BlogPost != null)
|
||||
{
|
||||
<PageTitle>@BlogPost.Title</PageTitle>
|
||||
<HeadContent>
|
||||
<meta property="og:title" content="@BlogPost.Title" />
|
||||
<meta property="og:description" content="@(new string(BlogPost.Text.Take(100).ToArray()))" />
|
||||
<meta property="og:image" content="@($"{_navman.BaseUri}/pathtoanimage.png")" />
|
||||
<meta property="og:url" content="@_navman.Uri" />
|
||||
<meta name="twitter:card" content="@(new string(BlogPost.Text.Take(100).ToArray()))" />
|
||||
</HeadContent>
|
||||
|
||||
<h2>@BlogPost.Title</h2>
|
||||
@((MarkupString)BlogPost.Text)
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
public string BlogPostId { get; set; }
|
||||
|
||||
public BlogPost? BlogPost { get; set; }
|
||||
protected async override Task OnParametersSetAsync()
|
||||
{
|
||||
BlogPost = await _api.GetBlogPostAsync(BlogPostId);
|
||||
await base.OnParametersSetAsync();
|
||||
}
|
||||
|
||||
}
|
||||
8
Chapter05/MyBlog/Components/Pages/SetFocus.razor
Normal file
8
Chapter05/MyBlog/Components/Pages/SetFocus.razor
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
@page "/setfocus"
|
||||
|
||||
<input @ref="textInput" />
|
||||
<button @onclick="() => textInput.FocusAsync()">Set focus</button>
|
||||
|
||||
@code {
|
||||
ElementReference textInput;
|
||||
}
|
||||
20
Chapter05/MyBlog/Components/RazorComponents/Alert.razor
Normal file
20
Chapter05/MyBlog/Components/RazorComponents/Alert.razor
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<div class="@($"alert alert-{Style.ToString().ToLower()}")" role="alert">
|
||||
@ChildContent
|
||||
</div>
|
||||
@code{
|
||||
[Parameter]
|
||||
public RenderFragment ChildContent { get; set; } =@<b>This is a default value</b>;
|
||||
[Parameter]
|
||||
public AlertStyle Style { get; set; }
|
||||
public enum AlertStyle
|
||||
{
|
||||
Primary,
|
||||
Secondary,
|
||||
Success,
|
||||
Danger,
|
||||
Warning,
|
||||
Info,
|
||||
Light,
|
||||
Dark
|
||||
}
|
||||
}
|
||||
17
Chapter05/MyBlog/Components/Shared/MainLayout.razor
Normal file
17
Chapter05/MyBlog/Components/Shared/MainLayout.razor
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
@inherits LayoutComponentBase
|
||||
|
||||
<div class="page">
|
||||
<div class="sidebar">
|
||||
<NavMenu />
|
||||
</div>
|
||||
|
||||
<main>
|
||||
<div class="top-row px-4">
|
||||
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
|
||||
</div>
|
||||
|
||||
<article class="content px-4">
|
||||
@Body
|
||||
</article>
|
||||
</main>
|
||||
</div>
|
||||
81
Chapter05/MyBlog/Components/Shared/MainLayout.razor.css
Normal file
81
Chapter05/MyBlog/Components/Shared/MainLayout.razor.css
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
.page {
|
||||
position: relative;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
main {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
|
||||
}
|
||||
|
||||
.top-row {
|
||||
background-color: #f7f7f7;
|
||||
border-bottom: 1px solid #d6d5d5;
|
||||
justify-content: flex-end;
|
||||
height: 3.5rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.top-row ::deep a, .top-row ::deep .btn-link {
|
||||
white-space: nowrap;
|
||||
margin-left: 1.5rem;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.top-row ::deep a:hover, .top-row ::deep .btn-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.top-row ::deep a:first-child {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
@media (max-width: 640.98px) {
|
||||
.top-row:not(.auth) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.top-row.auth {
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.top-row ::deep a, .top-row ::deep .btn-link {
|
||||
margin-left: 0;
|
||||
}
|
||||
}
|
||||
|
||||
@media (min-width: 641px) {
|
||||
.page {
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
width: 250px;
|
||||
height: 100vh;
|
||||
position: sticky;
|
||||
top: 0;
|
||||
}
|
||||
|
||||
.top-row {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.top-row.auth ::deep a:first-child {
|
||||
flex: 1;
|
||||
text-align: right;
|
||||
width: 0;
|
||||
}
|
||||
|
||||
.top-row, article {
|
||||
padding-left: 2rem !important;
|
||||
padding-right: 1.5rem !important;
|
||||
}
|
||||
}
|
||||
39
Chapter05/MyBlog/Components/Shared/NavMenu.razor
Normal file
39
Chapter05/MyBlog/Components/Shared/NavMenu.razor
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<div class="top-row ps-3 navbar navbar-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="">BlazorWebAssembly</a>
|
||||
<button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="@NavMenuCssClass nav-scrollable" @onclick="ToggleNavMenu">
|
||||
<nav class="flex-column">
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
|
||||
<span class="oi oi-home" aria-hidden="true"></span> Home
|
||||
</NavLink>
|
||||
</div>
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="counter">
|
||||
<span class="oi oi-plus" aria-hidden="true"></span> Counter
|
||||
</NavLink>
|
||||
</div>
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="fetchdata">
|
||||
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
|
||||
</NavLink>
|
||||
</div>
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
private bool collapseNavMenu = true;
|
||||
|
||||
private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null;
|
||||
|
||||
private void ToggleNavMenu()
|
||||
{
|
||||
collapseNavMenu = !collapseNavMenu;
|
||||
}
|
||||
}
|
||||
68
Chapter05/MyBlog/Components/Shared/NavMenu.razor.css
Normal file
68
Chapter05/MyBlog/Components/Shared/NavMenu.razor.css
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
.navbar-toggler {
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.top-row {
|
||||
height: 3.5rem;
|
||||
background-color: rgba(0,0,0,0.4);
|
||||
}
|
||||
|
||||
.navbar-brand {
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.oi {
|
||||
width: 2rem;
|
||||
font-size: 1.1rem;
|
||||
vertical-align: text-top;
|
||||
top: -2px;
|
||||
}
|
||||
|
||||
.nav-item {
|
||||
font-size: 0.9rem;
|
||||
padding-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.nav-item:first-of-type {
|
||||
padding-top: 1rem;
|
||||
}
|
||||
|
||||
.nav-item:last-of-type {
|
||||
padding-bottom: 1rem;
|
||||
}
|
||||
|
||||
.nav-item ::deep a {
|
||||
color: #d7d7d7;
|
||||
border-radius: 4px;
|
||||
height: 3rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
line-height: 3rem;
|
||||
}
|
||||
|
||||
.nav-item ::deep a.active {
|
||||
background-color: rgba(255,255,255,0.25);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.nav-item ::deep a:hover {
|
||||
background-color: rgba(255,255,255,0.1);
|
||||
color: white;
|
||||
}
|
||||
|
||||
@media (min-width: 641px) {
|
||||
.navbar-toggler {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.collapse {
|
||||
/* Never collapse the sidebar for wide screens */
|
||||
display: block;
|
||||
}
|
||||
|
||||
.nav-scrollable {
|
||||
/* Allow sidebar to scroll for tall menus */
|
||||
height: calc(100vh - 3.5rem);
|
||||
overflow-y: auto;
|
||||
}
|
||||
}
|
||||
16
Chapter05/MyBlog/Components/Shared/SurveyPrompt.razor
Normal file
16
Chapter05/MyBlog/Components/Shared/SurveyPrompt.razor
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<div class="alert alert-secondary mt-4">
|
||||
<span class="oi oi-pencil me-2" aria-hidden="true"></span>
|
||||
<strong>@Title</strong>
|
||||
|
||||
<span class="text-nowrap">
|
||||
Please take our
|
||||
<a target="_blank" class="font-weight-bold link-dark" href="https://go.microsoft.com/fwlink/?linkid=2186157">brief survey</a>
|
||||
</span>
|
||||
and tell us what you think.
|
||||
</div>
|
||||
|
||||
@code {
|
||||
// Demonstrates how a parent component can supply parameters
|
||||
[Parameter]
|
||||
public string? Title { get; set; }
|
||||
}
|
||||
11
Chapter05/MyBlog/Components/_Imports.razor
Normal file
11
Chapter05/MyBlog/Components/_Imports.razor
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
@using System.Net.Http
|
||||
@using Microsoft.AspNetCore.Authorization
|
||||
@using Microsoft.AspNetCore.Components.Forms
|
||||
@using Microsoft.AspNetCore.Components.Routing
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using Microsoft.AspNetCore.Components.Web.Virtualization
|
||||
@using Microsoft.JSInterop
|
||||
@using Components.Pages
|
||||
@using Components.Shared
|
||||
@using Data.Models.Interfaces
|
||||
@using Data.Models
|
||||
BIN
Chapter05/MyBlog/Components/wwwroot/background.png
Normal file
BIN
Chapter05/MyBlog/Components/wwwroot/background.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 378 B |
6
Chapter05/MyBlog/Components/wwwroot/exampleJsInterop.js
Normal file
6
Chapter05/MyBlog/Components/wwwroot/exampleJsInterop.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// This is a JavaScript module that is loaded on demand. It can export any number of
|
||||
// functions, and may import other JavaScript modules if required.
|
||||
|
||||
export function showPrompt(message) {
|
||||
return prompt(message, 'Type anything here');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue