Skip to content

Commit 1123168

Browse files
Andrew JankAndrew Jank
authored andcommitted
final updates to sample now that session is fixed, added README
1 parent 3799b7e commit 1123168

File tree

10 files changed

+84
-13
lines changed

10 files changed

+84
-13
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Globalization;
2+
using System.Text.RegularExpressions;
3+
4+
namespace BlazorSessionCore.Components
5+
{
6+
internal sealed class AxdContraint : IRouteConstraint
7+
{
8+
private readonly Regex regex;
9+
10+
public AxdContraint()
11+
{
12+
regex = new Regex(@"^*.axd", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(100));
13+
}
14+
15+
public bool Match(HttpContext? httpContext, IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
16+
{
17+
var isMatch = false;
18+
if (values.TryGetValue(routeKey, out object? value))
19+
{
20+
var parameterValueString = Convert.ToString(value, CultureInfo.InvariantCulture);
21+
if (parameterValueString != null)
22+
{
23+
isMatch = regex.IsMatch(parameterValueString);
24+
}
25+
}
26+
27+
return isMatch;
28+
}
29+
}
30+
}

samples/WebFormsToBlazorWithSession/BlazorSessionCore/BlazorSessionCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Yarp.ReverseProxy" Version="2.0.1" />
10+
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

samples/WebFormsToBlazorWithSession/BlazorSessionCore/Components/Pages/Home.razor

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44

55
<h1>Hello, world!</h1>
66

7-
Welcome to your new app.
8-
9-
<div>
10-
<label for="session-string">Session String:</label>
11-
<input name="session-string" type="text" @bind-value="testValue" />
12-
<button type="button" @onclick="OnSubmit">Submit</button>
7+
<div class="container-fluid">
8+
<EditForm Model="SessionData" OnSubmit="OnSubmit" class="col-md-4" FormName="Session">
9+
<div class="mb-2">
10+
<label for="session-message" class="form-label">Session Message:</label>
11+
<InputText id="session-message" class="form-control" @bind-Value="SessionData.SessionMessage" />
12+
</div>
13+
<div class="mb-2">
14+
<button type="submit" class="btn btn-primary">Submit</button>
15+
</div>
16+
</EditForm>
1317
</div>
1418

1519
@code {
16-
private string? testValue;
20+
[SupplyParameterFromForm]
21+
private SessionData SessionData { get; set; } = new();
1722

1823
[CascadingParameter]
1924
public HttpContext? HttpContext { get; set; }
@@ -23,9 +28,9 @@ Welcome to your new app.
2328
if (HttpContext != null)
2429
{
2530
var webHttpContext = ((System.Web.HttpContext)HttpContext);
26-
if (webHttpContext != null && webHttpContext.Session != null)
31+
if (webHttpContext != null && webHttpContext.Session != null && string.IsNullOrEmpty(SessionData.SessionMessage))
2732
{
28-
testValue = webHttpContext.Session["test-value"]?.ToString();
33+
SessionData.SessionMessage = webHttpContext.Session["test-value"]?.ToString() ?? string.Empty;
2934
}
3035
}
3136

@@ -39,7 +44,7 @@ Welcome to your new app.
3944
var webHttpContext = ((System.Web.HttpContext)HttpContext);
4045
if (webHttpContext != null && webHttpContext.Session != null)
4146
{
42-
webHttpContext.Session["test-value"] = testValue;
47+
webHttpContext.Session["test-value"] = SessionData.SessionMessage;
4348
}
4449
}
4550
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace BlazorSessionCore.Components
4+
{
5+
internal sealed class SessionData
6+
{
7+
public SessionData()
8+
{
9+
SessionMessage = string.Empty;
10+
}
11+
12+
public string SessionMessage { get; set; }
13+
}
14+
}

samples/WebFormsToBlazorWithSession/BlazorSessionCore/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// Add YARP
66
builder.Services.AddHttpForwarder();
77

8+
// Add constraint to redirect .axd files to WebForms
9+
builder.Services.AddRouting(options => options.ConstraintMap.Add("isAxdFile", typeof(AxdContraint)));
810
// Add System Web Adapters and setup session
911
builder.Services.AddSystemWebAdapters()
1012
.AddJsonSessionSerializer(options =>
@@ -46,4 +48,5 @@
4648
app.MapForwarder("/Content/{**catch-all}", app.Configuration["ProxyTo"]).Add(static builder => ((RouteEndpointBuilder)builder).Order = int.MaxValue);
4749
app.MapForwarder("/About", app.Configuration["ProxyTo"]).Add(static builder => ((RouteEndpointBuilder)builder).Order = int.MaxValue);
4850
app.MapForwarder("/Contact", app.Configuration["ProxyTo"]).Add(static builder => ((RouteEndpointBuilder)builder).Order = int.MaxValue);
51+
app.MapForwarder("/{route:isAxdFile}", app.Configuration["ProxyTo"]).Add(static builder => ((RouteEndpointBuilder)builder).Order = int.MaxValue);
4952
app.Run();
Loading
37.6 KB
Loading
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# WebForms Migration to Blazor with Session
2+
3+
Migrating WebForms to .NET Core is not a straightforward approach. This sample shows how the incremental migration can be used to share session state between Blazor and WebForms.
4+
5+
Session (and HttpContext) can only be obtained through Static SSR. See [documentation](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-8.0#ihttpcontextaccessorhttpcontext-in-razor-components-blazor).
6+
7+
Things to look at in this sample:
8+
9+
- [Program.cs](BlazorSessionCore/Program.cs) and [Global.asax.cs](WebFormsSessionFramework/Global.asax.cs): This is where the remote app session state is initialized for the Blazor and WebForms app respectively. See [documentation](https://learn.microsoft.com/en-us/aspnet/core/migration/inc/remote-session) on how this works.
10+
- [AxdConstraint.cs](BlazorSessionCore/AxdConstraint.cs): An AXD file is a file use by ASP.NET applications for handling embedded resource requests. It contains instructions for retrieving embedded resources, such as images, JavaScript (.JS) files, and.CSS files. AXD files are used for injecting resources into the client-side webpage and access them on the server in a standard way. This constraint allows AXD files to be retrieved from WebForms.
11+
12+
The session can be updated from Blazor on the main page:
13+
14+
![Main page screenshot](MainPage.png)
15+
16+
17+
The session can be updated from WebForms on the Contact page:
18+
19+
![Contact page screenshot](ContactPage.png)

samples/WebFormsToBlazorWithSession/WebFormsSessionFramework/Contact.aspx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</address>
1818

1919
<div>
20-
<p>Session String:<asp:TextBox ID="SessionTextBox" runat="server" /></p>
20+
<p>Session Message:<asp:TextBox ID="SessionTextBox" runat="server" /></p>
2121
<asp:Button ID="SubmitSessionButton" runat="server" Text="Submit" OnClick="SubmitSessionButton_Click" />
2222
</div>
2323
</main>

samples/WebFormsToBlazorWithSession/WebFormsSessionFramework/Contact.aspx.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ protected void Page_Load(object sender, EventArgs e)
1313
{
1414
if (!IsPostBack)
1515
{
16-
SessionTextBox.Text = string.Empty;
16+
SessionTextBox.Text = Session["test-value"]?.ToString() ?? string.Empty;
1717
}
1818
}
1919

0 commit comments

Comments
 (0)