Skip to content

Commit a27e4c4

Browse files
Supply HttpContext as cascading value
1 parent a8acc60 commit a27e4c4

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/Components/Endpoints/src/DependencyInjection/RazorComponentsServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public static IRazorComponentsBuilder AddRazorComponents(this IServiceCollection
6363
services.TryAddScoped<EndpointRoutingStateProvider>();
6464
services.TryAddScoped<IRoutingStateProvider>(sp => sp.GetRequiredService<EndpointRoutingStateProvider>());
6565
services.AddSupplyValueFromQueryProvider();
66+
services.AddCascadingValue(sp => sp.GetRequiredService<EndpointHtmlRenderer>().HttpContext);
6667

6768
// Form handling
6869
services.AddSupplyValueFromFormProvider();

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public EndpointHtmlRenderer(IServiceProvider serviceProvider, ILoggerFactory log
5252
_services = serviceProvider;
5353
}
5454

55+
internal HttpContext? HttpContext => _httpContext;
56+
5557
private void SetHttpContext(HttpContext httpContext)
5658
{
5759
if (_httpContext is null)

src/Components/test/E2ETest/ServerRenderingTests/RenderingTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Net;
5+
using System.Net.Http;
46
using Components.TestServer.RazorComponents;
57
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
68
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
@@ -35,4 +37,16 @@ public void CanRenderLargeComponentsWithServerRenderMode()
3537
Assert.Equal(result, Browser.FindElement(By.Id("server-prerender")).Text);
3638
Assert.Equal(result, Browser.FindElement(By.Id("server-prerender")).Text);
3739
}
40+
41+
[Fact]
42+
public async Task CanUseHttpContextRequestAndResponse()
43+
{
44+
Navigate($"{ServerPathBase}/httpcontext");
45+
Browser.Equal("GET", () => Browser.FindElement(By.Id("request-method")).Text);
46+
Browser.Equal("/httpcontext", () => Browser.FindElement(By.Id("request-path")).Text);
47+
48+
// We can't see the response status code using Selenium, so make a direct request
49+
var response = await new HttpClient().GetAsync(Browser.Url);
50+
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
51+
}
3852
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@page "/httpcontext"
2+
3+
<h1>HttpContext</h1>
4+
5+
<p>
6+
Request method: <span id="request-method">@Ctx.Request.Method</span>
7+
</p>
8+
<p>
9+
Request path: <span id="request-path">@Ctx.Request.Path</span>
10+
</p>
11+
12+
@code {
13+
[CascadingParameter] public HttpContext Ctx { get; set; }
14+
15+
protected override void OnInitialized()
16+
{
17+
// Show we can change the response status code
18+
Ctx.Response.StatusCode = 201;
19+
}
20+
}

0 commit comments

Comments
 (0)