Skip to content

Commit b3afb1b

Browse files
committed
Add sample for HttpContext access
1 parent 690e7e2 commit b3afb1b

File tree

11 files changed

+111
-4
lines changed

11 files changed

+111
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Samples for ASP.NET Core 6.0, 7.0 and 8.0 RC 2 (502)
1+
# Samples for ASP.NET Core 6.0, 7.0 and 8.0 RC 2 (503)
22

3-
- Samples for ASP.NET Core **8.0 RC 2** is available [here](/projects/.net8) (39).
3+
- Samples for ASP.NET Core **8.0 RC 2** is available [here](/projects/.net8) (40).
44
- Samples for ASP.NET Core **7.0** is available [here](/projects/.net7) (45).
55
- Samples for ASP.NET Core **8.0 Preview 6** using EdgeDB.NET is [here](https://github.com/edgedb/edgedb-net).
66

projects/.net8/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ASP.NET 8.0 Preview 7 (39)
1+
# ASP.NET 8.0 Preview 7 (40)
22

33
These samples require [.NET 8.0 RC 2](https://github.com/dotnet/installer#table).
44

@@ -46,10 +46,14 @@ These samples require [.NET 8.0 RC 2](https://github.com/dotnet/installer#table)
4646

4747
This sample demonstrates a Razor Component Page SSR that handle a POST form, hosts "number" component with with Streaming rendering and hosts "interactive" components backed by Blazor Web Assembly and Blazor Server.
4848

49-
* [RazorComponentElevent](RazorComponentEleven)
49+
* [RazorComponentEleven](RazorComponentEleven)
5050

5151
This sample shows how to update the UI multiple times using `StateHasChanged();` while in streaming rendering mode.
5252

53+
* [RazorComponentTwelve](RazorComponentTwelve)
54+
55+
This sample shows how to access `HttpContext` from a static Razor component.
56+
5357
* [Short Circuit](map-short-circuit)
5458

5559
Use `MapShortCircuit` or `.ShortCircuit()` to efficiently respond to a request without going through a middleware pipeline run.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dotnet.defaultSolution": "RazorComponentEleven.sln"
3+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
6+
<title>HttpContext</title>
7+
<base href="/" />
8+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
9+
<HeadOutlet />
10+
</head>
11+
<body>
12+
<Router AppAssembly="@typeof(App).Assembly">
13+
<Found Context="routeData">
14+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
15+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
16+
</Found>
17+
</Router>
18+
19+
<script src="_framework/blazor.web.js" suppress-error="BL9992"></script>
20+
</body>
21+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@page "/"
2+
3+
@_message
4+
5+
<br/>
6+
7+
HttpContext.Request.Path @HttpContext!.Request.Path
8+
9+
@code
10+
{
11+
[CascadingParameter]
12+
public HttpContext? HttpContext { get; set; }
13+
14+
string _message = string.Empty;
15+
protected override void OnInitialized()
16+
{
17+
_message = HttpContext is object ? "HttpContext exists at OnInitialized" : "HttpContext is null";
18+
}
19+
}
20+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var builder = WebApplication.CreateBuilder();
2+
builder.Services.AddRazorComponents();
3+
builder.Services.AddAntiforgery();
4+
var app = builder.Build();
5+
app.UseAntiforgery();
6+
app.MapRazorComponents<RazorComponentTwelve.App>();
7+
app.Run();
8+
9+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Accessing HttpContext on static Razor Component
2+
3+
This sample shows how to access `HttpContext` from a static Razor component.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<ImplicitUsings>true</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RazorComponentTwelve", "RazorComponentTwelve.csproj", "{637DBFE0-E61B-47FB-9B50-8F872517F51B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{637DBFE0-E61B-47FB-9B50-8F872517F51B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{637DBFE0-E61B-47FB-9B50-8F872517F51B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{637DBFE0-E61B-47FB-9B50-8F872517F51B}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{637DBFE0-E61B-47FB-9B50-8F872517F51B}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {8CD56AB3-3C87-451F-9D64-BE14D1F98316}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@inherits LayoutComponentBase
2+
3+
<div class="container">
4+
@Body
5+
</div>

0 commit comments

Comments
 (0)