Skip to content

Commit d7fb6d8

Browse files
committed
feat: HelloBlazorServer.csproj - template-based sample created.
1 parent 897b385 commit d7fb6d8

31 files changed

+1316
-0
lines changed

Samples.sln

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.Blazor.Server", "sr
1616
EndProject
1717
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld", "src\HelloWorld\HelloWorld.csproj", "{E43053F9-B7F9-4A57-B4B7-2E84C3179D0B}"
1818
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloBlazorServer", "src\HelloBlazorServer\HelloBlazorServer.csproj", "{D347CCA4-0F31-415A-86EB-8C85C645AD22}"
20+
EndProject
1921
Global
2022
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2123
Debug|Any CPU = Debug|Any CPU
@@ -28,6 +30,7 @@ Global
2830
{B3AC9B52-96D7-4CBD-9873-8B427BB09BEB} = {01FE6776-E788-46B2-93E4-1CEDB01AA851}
2931
{9B29BD3F-7D94-4936-8379-61A2F48259AF} = {B5A8469D-C4BB-4808-8E44-FF4DFCA57BDE}
3032
{E43053F9-B7F9-4A57-B4B7-2E84C3179D0B} = {B5A8469D-C4BB-4808-8E44-FF4DFCA57BDE}
33+
{D347CCA4-0F31-415A-86EB-8C85C645AD22} = {B5A8469D-C4BB-4808-8E44-FF4DFCA57BDE}
3134
EndGlobalSection
3235
GlobalSection(ProjectConfigurationPlatforms) = postSolution
3336
{BA3D19EB-DDFD-4DB6-B60F-A110E22E9C48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
@@ -50,5 +53,9 @@ Global
5053
{E43053F9-B7F9-4A57-B4B7-2E84C3179D0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
5154
{E43053F9-B7F9-4A57-B4B7-2E84C3179D0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
5255
{E43053F9-B7F9-4A57-B4B7-2E84C3179D0B}.Release|Any CPU.Build.0 = Release|Any CPU
56+
{D347CCA4-0F31-415A-86EB-8C85C645AD22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57+
{D347CCA4-0F31-415A-86EB-8C85C645AD22}.Debug|Any CPU.Build.0 = Debug|Any CPU
58+
{D347CCA4-0F31-415A-86EB-8C85C645AD22}.Release|Any CPU.ActiveCfg = Release|Any CPU
59+
{D347CCA4-0F31-415A-86EB-8C85C645AD22}.Release|Any CPU.Build.0 = Release|Any CPU
5360
EndGlobalSection
5461
EndGlobal

src/HelloBlazorServer/App.razor

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
<Router AppAssembly="@typeof(Program).Assembly">
3+
<Found Context="routeData">
4+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
5+
</Found>
6+
<NotFound>
7+
<LayoutView Layout="@typeof(MainLayout)">
8+
<p>Sorry, there's nothing at this address.</p>
9+
</LayoutView>
10+
</NotFound>
11+
</Router>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace Samples.HelloBlazorServer.Data
4+
{
5+
public class WeatherForecast
6+
{
7+
public DateTime Date { get; set; }
8+
9+
public int TemperatureC { get; set; }
10+
11+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
12+
13+
public string Summary { get; set; }
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
5+
namespace Samples.HelloBlazorServer.Data
6+
{
7+
public class WeatherForecastService
8+
{
9+
private static readonly string[] Summaries = new[]
10+
{
11+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12+
};
13+
14+
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
15+
{
16+
var rng = new Random();
17+
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
18+
{
19+
Date = startDate.AddDays(index),
20+
TemperatureC = rng.Next(-20, 55),
21+
Summary = Summaries[rng.Next(Summaries.Length)]
22+
}).ToArray());
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<AssemblyName>Samples.HelloBlazorServer</AssemblyName>
6+
<RootNamespace>Samples.HelloBlazorServer</RootNamespace>
7+
</PropertyGroup>
8+
9+
</Project>
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@page "/counter"
2+
3+
<h1>Counter</h1>
4+
5+
<p>Current count: @currentCount</p>
6+
7+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
8+
9+
@code {
10+
private int currentCount = 0;
11+
12+
private void IncrementCount()
13+
{
14+
currentCount++;
15+
}
16+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@page "/error"
2+
3+
4+
<h1 class="text-danger">Error.</h1>
5+
<h2 class="text-danger">An error occurred while processing your request.</h2>
6+
7+
<h3>Development Mode</h3>
8+
<p>
9+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
10+
</p>
11+
<p>
12+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
13+
It can result in displaying sensitive information from exceptions to end users.
14+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
15+
and restarting the app.
16+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@page "/fetchdata"
2+
3+
@using Samples.HelloBlazorServer.Data
4+
@inject WeatherForecastService ForecastService
5+
6+
<h1>Weather forecast</h1>
7+
8+
<p>This component demonstrates fetching data from a service.</p>
9+
10+
@if (forecasts == null)
11+
{
12+
<p><em>Loading...</em></p>
13+
}
14+
else
15+
{
16+
<table class="table">
17+
<thead>
18+
<tr>
19+
<th>Date</th>
20+
<th>Temp. (C)</th>
21+
<th>Temp. (F)</th>
22+
<th>Summary</th>
23+
</tr>
24+
</thead>
25+
<tbody>
26+
@foreach (var forecast in forecasts)
27+
{
28+
<tr>
29+
<td>@forecast.Date.ToShortDateString()</td>
30+
<td>@forecast.TemperatureC</td>
31+
<td>@forecast.TemperatureF</td>
32+
<td>@forecast.Summary</td>
33+
</tr>
34+
}
35+
</tbody>
36+
</table>
37+
}
38+
39+
@code {
40+
private WeatherForecast[] forecasts;
41+
42+
protected override async Task OnInitializedAsync()
43+
{
44+
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
45+
}
46+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@page "/"
2+
3+
<h1>Hello, world!</h1>
4+
5+
Welcome to your new app.
6+
7+
<SurveyPrompt Title="How is Blazor working for you?" />
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@page "/"
2+
@using Samples.HelloBlazorServer
3+
@namespace HelloBlazorServer.Pages
4+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
5+
@{
6+
Layout = null;
7+
}
8+
9+
<!DOCTYPE html>
10+
<html lang="en">
11+
<head>
12+
<meta charset="utf-8" />
13+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
14+
<title>HelloBlazorServer</title>
15+
<base href="~/" />
16+
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
17+
<link href="css/site.css" rel="stylesheet" />
18+
</head>
19+
<body>
20+
<app>
21+
<component type="typeof(App)" render-mode="ServerPrerendered" />
22+
</app>
23+
24+
<div id="blazor-error-ui">
25+
<environment include="Staging,Production">
26+
An error has occurred. This application may no longer respond until reloaded.
27+
</environment>
28+
<environment include="Development">
29+
An unhandled exception has occurred. See browser dev tools for details.
30+
</environment>
31+
<a href="" class="reload">Reload</a>
32+
<a class="dismiss">🗙</a>
33+
</div>
34+
35+
<script src="_framework/blazor.server.js"></script>
36+
</body>
37+
</html>

src/HelloBlazorServer/Program.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace Samples.HelloBlazorServer
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
CreateHostBuilder(args).Build().Run();
11+
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@inherits LayoutComponentBase
2+
3+
<div class="sidebar">
4+
<NavMenu />
5+
</div>
6+
7+
<div class="main">
8+
<div class="top-row px-4">
9+
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
10+
</div>
11+
12+
<div class="content px-4">
13+
@Body
14+
</div>
15+
</div>
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<div class="top-row pl-4 navbar navbar-dark">
2+
<a class="navbar-brand" href="">HelloBlazorServer</a>
3+
<button class="navbar-toggler" @onclick="ToggleNavMenu">
4+
<span class="navbar-toggler-icon"></span>
5+
</button>
6+
</div>
7+
8+
<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
9+
<ul class="nav flex-column">
10+
<li class="nav-item px-3">
11+
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
12+
<span class="oi oi-home" aria-hidden="true"></span> Home
13+
</NavLink>
14+
</li>
15+
<li class="nav-item px-3">
16+
<NavLink class="nav-link" href="counter">
17+
<span class="oi oi-plus" aria-hidden="true"></span> Counter
18+
</NavLink>
19+
</li>
20+
<li class="nav-item px-3">
21+
<NavLink class="nav-link" href="fetchdata">
22+
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
23+
</NavLink>
24+
</li>
25+
</ul>
26+
</div>
27+
28+
@code {
29+
private bool collapseNavMenu = true;
30+
31+
private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;
32+
33+
private void ToggleNavMenu()
34+
{
35+
collapseNavMenu = !collapseNavMenu;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<div class="alert alert-secondary mt-4" role="alert">
2+
<span class="oi oi-pencil mr-2" aria-hidden="true"></span>
3+
<strong>@Title</strong>
4+
5+
<span class="text-nowrap">
6+
Please take our
7+
<a target="_blank" class="font-weight-bold" href="https://go.microsoft.com/fwlink/?linkid=2112271">brief survey</a>
8+
</span>
9+
and tell us what you think.
10+
</div>
11+
12+
@code {
13+
// Demonstrates how a parent component can supply parameters
14+
[Parameter]
15+
public string Title { get; set; }
16+
}

src/HelloBlazorServer/Startup.cs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
6+
using Samples.HelloBlazorServer.Data;
7+
8+
namespace Samples.HelloBlazorServer
9+
{
10+
public class Startup
11+
{
12+
public Startup(IConfiguration configuration)
13+
{
14+
Configuration = configuration;
15+
}
16+
17+
public IConfiguration Configuration { get; }
18+
19+
// This method gets called by the runtime. Use this method to add services to the container.
20+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
21+
public void ConfigureServices(IServiceCollection services)
22+
{
23+
services.AddRazorPages();
24+
services.AddServerSideBlazor();
25+
services.AddSingleton<WeatherForecastService>();
26+
}
27+
28+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
29+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
30+
{
31+
if (env.IsDevelopment())
32+
{
33+
app.UseDeveloperExceptionPage();
34+
}
35+
else
36+
{
37+
app.UseExceptionHandler("/Error");
38+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
39+
app.UseHsts();
40+
}
41+
42+
app.UseHttpsRedirection();
43+
app.UseStaticFiles();
44+
45+
app.UseRouting();
46+
47+
app.UseEndpoints(endpoints =>
48+
{
49+
endpoints.MapBlazorHub();
50+
endpoints.MapFallbackToPage("/_Host");
51+
});
52+
}
53+
}
54+
}

src/HelloBlazorServer/_Imports.razor

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@using System.Net.Http
2+
@using Microsoft.AspNetCore.Authorization
3+
@using Microsoft.AspNetCore.Components.Authorization
4+
@using Microsoft.AspNetCore.Components.Forms
5+
@using Microsoft.AspNetCore.Components.Routing
6+
@using Microsoft.AspNetCore.Components.Web
7+
@using Microsoft.JSInterop
8+
@using Samples.HelloBlazorServer
9+
@using Samples.HelloBlazorServer.Shared
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"DetailedErrors": true,
3+
"Logging": {
4+
"LogLevel": {
5+
"Default": "Information",
6+
"Microsoft": "Warning",
7+
"Microsoft.Hosting.Lifetime": "Information"
8+
}
9+
}
10+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}

0 commit comments

Comments
 (0)