Skip to content

Commit c53bd8f

Browse files
Prerenderer now passes original (unescaped) URL to Node - fixesaspnet#250
1 parent 0d0d25b commit c53bd8f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

‎src/Microsoft.AspNetCore.SpaServices/Prerendering/PrerenderTagHelper.cs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
usingSystem.Text;
33
usingSystem.Threading.Tasks;
44
usingMicrosoft.AspNetCore.Hosting;
5-
usingMicrosoft.AspNetCore.Http;
6-
usingMicrosoft.AspNetCore.Http.Extensions;
5+
usingMicrosoft.AspNetCore.Http.Features;
76
usingMicrosoft.AspNetCore.Mvc.ViewFeatures;
87
usingMicrosoft.AspNetCore.Mvc.Rendering;
98
usingMicrosoft.AspNetCore.NodeServices;
109
usingMicrosoft.AspNetCore.Razor.TagHelpers;
11-
usingMicrosoft.Extensions.PlatformAbstractions;
1210
usingNewtonsoft.Json;
1311

1412
namespaceMicrosoft.AspNetCore.SpaServices.Prerendering
@@ -60,7 +58,19 @@ public PrerenderTagHelper(IServiceProvider serviceProvider)
6058

6159
publicoverrideasyncTaskProcessAsync(TagHelperContextcontext,TagHelperOutputoutput)
6260
{
61+
// We want to pass the original, unencoded incoming URL data through to Node, so that
62+
// server-side code has the same view of the URL as client-side code (on the client,
63+
// location.pathname returns an unencoded string).
64+
// The following logic handles special characters in URL paths in the same way that
65+
// Node and client-side JS does. For example, the path "/a=b%20c" gets passed through
66+
// unchanged (whereas other .NET APIs do change it - Path.Value will return it as
67+
// "/a=b c" and Path.ToString() will return it as "/a%3db%20c")
68+
varrequestFeature=ViewContext.HttpContext.Features.Get<IHttpRequestFeature>();
69+
varunencodedPathAndQuery=requestFeature.RawTarget;
70+
6371
varrequest=ViewContext.HttpContext.Request;
72+
varunencodedAbsoluteUrl=$"{request.Scheme}://{request.Host}{unencodedPathAndQuery}";
73+
6474
varresult=awaitPrerenderer.RenderToString(
6575
_applicationBasePath,
6676
_nodeServices,
@@ -69,8 +79,8 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
6979
ExportName=ExportName,
7080
WebpackConfig=WebpackConfigPath
7181
},
72-
request.GetEncodedUrl(),
73-
request.Path+request.QueryString.Value,
82+
unencodedAbsoluteUrl,
83+
unencodedPathAndQuery,
7484
CustomDataParameter);
7585
output.Content.SetHtmlContent(result.Html);
7686

0 commit comments

Comments
(0)