Skip to content

Commit

Permalink
Supply HttpContext as cascading value
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSandersonMS committed Aug 22, 2023
1 parent a8acc60 commit 2b670d2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
Expand Down Expand Up @@ -63,6 +64,7 @@ public static IRazorComponentsBuilder AddRazorComponents(this IServiceCollection
services.TryAddScoped<EndpointRoutingStateProvider>();
services.TryAddScoped<IRoutingStateProvider>(sp => sp.GetRequiredService<EndpointRoutingStateProvider>());
services.AddSupplyValueFromQueryProvider();
services.AddCascadingValue(sp => sp.GetRequiredService<EndpointHtmlRenderer>().HttpContext);

// Form handling
services.AddSupplyValueFromFormProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public EndpointHtmlRenderer(IServiceProvider serviceProvider, ILoggerFactory log
_services = serviceProvider;
}

internal HttpContext? HttpContext => _httpContext;

private void SetHttpContext(HttpContext httpContext)
{
if (_httpContext is null)
Expand Down
14 changes: 14 additions & 0 deletions src/Components/test/E2ETest/ServerRenderingTests/RenderingTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net;
using System.Net.Http;
using Components.TestServer.RazorComponents;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
Expand Down Expand Up @@ -35,4 +37,16 @@ public void CanRenderLargeComponentsWithServerRenderMode()
Assert.Equal(result, Browser.FindElement(By.Id("server-prerender")).Text);
Assert.Equal(result, Browser.FindElement(By.Id("server-prerender")).Text);
}

[Fact]
public async Task CanUseHttpContextRequestAndResponse()
{
Navigate($"{ServerPathBase}/httpcontext");
Browser.Equal("GET", () => Browser.FindElement(By.Id("request-method")).Text);
Browser.Equal("/httpcontext", () => Browser.FindElement(By.Id("request-path")).Text);

// We can't see the response status code using Selenium, so make a direct request
var response = await new HttpClient().GetAsync(Browser.Url);
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@page "/httpcontext"

<h1>HttpContext</h1>

<p>
Request method: <span id="request-method">@Ctx.Request.Method</span>
</p>
<p>
Request path: <span id="request-path">@Ctx.Request.Path</span>
</p>

@code {
[CascadingParameter] public HttpContext Ctx { get; set; }

protected override void OnInitialized()
{
// Show we can change the response status code
Ctx.Response.StatusCode = 201;
}
}

0 comments on commit 2b670d2

Please sign in to comment.