Skip to content

Commit 4e62e3c

Browse files
Added support for relative paths (non-root webapplications) (#1141)
Co-authored-by: Kenzo De Ridder <[email protected]>
1 parent 9d099d0 commit 4e62e3c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/React.AspNet/HtmlHelperExtensions.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
#if LEGACYASPNET
1414
using System.Web;
1515
using IHtmlHelper = System.Web.Mvc.HtmlHelper;
16+
using IUrlHelper = System.Web.Mvc.UrlHelper;
1617
#else
1718
using Microsoft.AspNetCore.Mvc.Rendering;
1819
using Microsoft.AspNetCore.Html;
1920
using IHtmlString = Microsoft.AspNetCore.Html.IHtmlContent;
21+
using Microsoft.AspNetCore.Mvc;
2022
#endif
2123

2224
#if LEGACYASPNET
@@ -148,7 +150,7 @@ public static IHtmlString ReactWithInit<T>(
148150
{
149151
Environment.ReturnEngineToPool();
150152
}
151-
}
153+
}
152154

153155
/// <summary>
154156
/// Renders the JavaScript required to initialise all components client-side. This will
@@ -174,26 +176,28 @@ public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper, bool
174176
/// Returns script tags based on the webpack asset manifest
175177
/// </summary>
176178
/// <param name="htmlHelper"></param>
179+
/// <param name="urlHelper">Optional IUrlHelper instance. Enables the use of tilde/relative (~/) paths inside the expose-components.js file.</param>
177180
/// <returns></returns>
178-
public static IHtmlString ReactGetScriptPaths(this IHtmlHelper htmlHelper)
181+
public static IHtmlString ReactGetScriptPaths(this IHtmlHelper htmlHelper, IUrlHelper urlHelper = null)
179182
{
180183
string nonce = Environment.Configuration.ScriptNonceProvider != null
181184
? $" nonce=\"{Environment.Configuration.ScriptNonceProvider()}\""
182185
: "";
183186

184187
return new HtmlString(string.Join("", Environment.GetScriptPaths()
185-
.Select(scriptPath => $"<script{nonce} src=\"{scriptPath}\"></script>")));
188+
.Select(scriptPath => $"<script{nonce} src=\"{(urlHelper == null ? scriptPath : urlHelper.Content(scriptPath))}\"></script>")));
186189
}
187190

188191
/// <summary>
189192
/// Returns style tags based on the webpack asset manifest
190193
/// </summary>
191194
/// <param name="htmlHelper"></param>
195+
/// <param name="urlHelper">Optional IUrlHelper instance. Enables the use of tilde/relative (~/) paths inside the expose-components.js file.</param>
192196
/// <returns></returns>
193-
public static IHtmlString ReactGetStylePaths(this IHtmlHelper htmlHelper)
197+
public static IHtmlString ReactGetStylePaths(this IHtmlHelper htmlHelper, IUrlHelper urlHelper = null)
194198
{
195199
return new HtmlString(string.Join("", Environment.GetStylePaths()
196-
.Select(stylePath => $"<link rel=\"stylesheet\" href=\"{stylePath}\" />")));
200+
.Select(stylePath => $"<link rel=\"stylesheet\" href=\"{(urlHelper == null ? stylePath : urlHelper.Content(stylePath))}\" />")));
197201
}
198202

199203
private static IHtmlString RenderToString(Action<StringWriter> withWriter)

0 commit comments

Comments
 (0)