Skip to content

Commit 2b670d2

Browse files
Supply HttpContext as cascading value
1 parent a8acc60 commit 2b670d2

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.AspNetCore.Components.Routing;
1313
using Microsoft.AspNetCore.Components.Web;
1414
using Microsoft.AspNetCore.DataProtection;
15+
using Microsoft.AspNetCore.Http;
1516
using Microsoft.Extensions.DependencyInjection.Extensions;
1617
using Microsoft.Extensions.Options;
1718
using Microsoft.JSInterop;
@@ -63,6 +64,7 @@ public static IRazorComponentsBuilder AddRazorComponents(this IServiceCollection
6364
services.TryAddScoped<EndpointRoutingStateProvider>();
6465
services.TryAddScoped<IRoutingStateProvider>(sp => sp.GetRequiredService<EndpointRoutingStateProvider>());
6566
services.AddSupplyValueFromQueryProvider();
67+
services.AddCascadingValue(sp => sp.GetRequiredService<EndpointHtmlRenderer>().HttpContext);
6668

6769
// Form handling
6870
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)