/
WebAssemblyNetDebugProxyAppBuilderExtensions.cs
46 lines (43 loc) · 1.82 KB
/
WebAssemblyNetDebugProxyAppBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Net;
namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// Provides infrastructure for debugging Blazor WebAssembly applications.
/// </summary>
public static class WebAssemblyNetDebugProxyAppBuilderExtensions
{
/// <summary>
/// Adds middleware for needed for debugging Blazor WebAssembly applications
/// inside Chromium dev tools.
/// </summary>
public static void UseWebAssemblyDebugging(this IApplicationBuilder app)
{
app.Map("/_framework/debug", app =>
{
app.Use(async (context, next) =>
{
var debugProxyBaseUrl = await DebugProxyLauncher.EnsureLaunchedAndGetUrl(context.RequestServices);
var requestPath = context.Request.Path.ToString();
if (requestPath == string.Empty)
{
requestPath = "/";
}
// Although we could redirect for every URL we see here, we filter the allowed set
// to ensure this doesn't get misused as some kind of more general redirector
switch (requestPath)
{
case "/":
case "/ws-proxy":
context.Response.Redirect($"{debugProxyBaseUrl}{requestPath}{context.Request.QueryString}");
break;
default:
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
break;
}
});
});
}
}
}