Commit 52f77b3
fix: ReferralMiddleware causing blank pages with rid query parameter (#757)
## Problem
Pages with `rid` query parameter (e.g., `/guidelines?rid=abc123`) were
returning blank content with HTTP 200 status, while the same pages
without the parameter worked correctly.
## Root Cause
The `ReferralMiddleware` was processing referral tracking but failing to
continue the request pipeline when a `rid` parameter was present. The
middleware was missing the critical `await _Next(context);` call after
referral processing, causing the request to terminate early.
## Before (Broken):
```csharp
public async Task InvokeAsync(HttpContext context, IReferralService referralService, UserManager<EssentialCSharpWebUser> userManager)
{
string? referralId = query["rid"];
if (string.IsNullOrWhiteSpace(referralId))
{
await _Next(context); // ✓ Continues pipeline when NO rid
return;
}
// Process referral tracking
referralService.TrackReferralAsync(referralId, claimsUser);
// ❌ MISSING: await _Next(context);
// Pipeline stops here when rid exists!
}
```
## After (Fixed):
```csharp
public async Task InvokeAsync(HttpContext context, IReferralService referralService, UserManager<EssentialCSharpWebUser> userManager)
{
string? referralId = query["rid"];
if (string.IsNullOrWhiteSpace(referralId))
{
await _Next(context);
return;
}
try
{
// Process referral tracking
referralService.TrackReferralAsync(referralId, claimsUser);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to track referral ID {ReferralId}", referralId);
}
// ✅ FIXED: Always continue the pipeline
await _Next(context);
}
```
## Changes Made
1. **Critical Fix**: Added missing `await _Next(context);` call after
referral processing
2. **Error Resilience**: Added try-catch around referral tracking to
prevent exceptions from breaking page rendering
3. **Logging Enhancement**: Added structured error logging for debugging
referral tracking issues
4. **Test Coverage**: Added comprehensive tests to verify the fix works
for all scenarios
## Verification
✅ `/guidelines?rid=abc123` now renders content correctly
✅ `/about?rid=user-ref` now renders content correctly
✅ Referral tracking functionality preserved
✅ Error scenarios handled gracefully
✅ Backward compatibility maintained
## Test Cases Added
- Pages with valid rid parameters return content
- Pages with empty/whitespace rid parameters work correctly
- Pages with non-rid parameters continue to work
- Error scenarios are logged but don't break page rendering
Fixes #756.
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 Share your feedback on Copilot coding agent for the chance to win a
$200 gift card! Click
[here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to
start the survey.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Co-authored-by: Benjamin Michaelis <git@relay.benjamin.michaelis.net>
Co-authored-by: Keboo <952248+Keboo@users.noreply.github.com>1 parent dc119f9 commit 52f77b3
File tree
3 files changed
+46
-4
lines changed- EssentialCSharp.Web.Tests
- EssentialCSharp.Web/Middleware
3 files changed
+46
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
23 | 48 | | |
24 | 49 | | |
25 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
11 | 15 | | |
12 | 16 | | |
13 | 17 | | |
| |||
21 | 25 | | |
22 | 26 | | |
23 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
24 | 31 | | |
25 | 32 | | |
26 | | - | |
| 33 | + | |
27 | 34 | | |
28 | 35 | | |
29 | 36 | | |
30 | | - | |
31 | 37 | | |
32 | 38 | | |
33 | 39 | | |
34 | 40 | | |
35 | | - | |
36 | 41 | | |
37 | 42 | | |
38 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
39 | 54 | | |
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | 3 | | |
| |||
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
| 37 | + | |
36 | 38 | | |
37 | 39 | | |
0 commit comments