From ad82bb7b497c71eb1226da07f84cfdef7aad9213 Mon Sep 17 00:00:00 2001 From: Kenzo De Ridder Date: Mon, 3 Aug 2020 07:40:10 +0200 Subject: [PATCH] Added support for relative paths (non-root webapplications) --- src/React.AspNet/HtmlHelperExtensions.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/React.AspNet/HtmlHelperExtensions.cs b/src/React.AspNet/HtmlHelperExtensions.cs index cb33e4cae..08336eefd 100644 --- a/src/React.AspNet/HtmlHelperExtensions.cs +++ b/src/React.AspNet/HtmlHelperExtensions.cs @@ -13,10 +13,12 @@ #if LEGACYASPNET using System.Web; using IHtmlHelper = System.Web.Mvc.HtmlHelper; +using IUrlHelper = System.Web.Mvc.UrlHelper; #else using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Html; using IHtmlString = Microsoft.AspNetCore.Html.IHtmlContent; +using Microsoft.AspNetCore.Mvc; #endif #if LEGACYASPNET @@ -148,7 +150,7 @@ public static IHtmlString ReactWithInit( { Environment.ReturnEngineToPool(); } - } + } /// /// Renders the JavaScript required to initialise all components client-side. This will @@ -174,26 +176,28 @@ public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper, bool /// Returns script tags based on the webpack asset manifest /// /// + /// Optional IUrlHelper instance. Enables the use of tilde/relative (~/) paths inside the expose-components.js file. /// - public static IHtmlString ReactGetScriptPaths(this IHtmlHelper htmlHelper) + public static IHtmlString ReactGetScriptPaths(this IHtmlHelper htmlHelper, IUrlHelper urlHelper = null) { string nonce = Environment.Configuration.ScriptNonceProvider != null ? $" nonce=\"{Environment.Configuration.ScriptNonceProvider()}\"" : ""; return new HtmlString(string.Join("", Environment.GetScriptPaths() - .Select(scriptPath => $""))); + .Select(scriptPath => $""))); } /// /// Returns style tags based on the webpack asset manifest /// /// + /// Optional IUrlHelper instance. Enables the use of tilde/relative (~/) paths inside the expose-components.js file. /// - public static IHtmlString ReactGetStylePaths(this IHtmlHelper htmlHelper) + public static IHtmlString ReactGetStylePaths(this IHtmlHelper htmlHelper, IUrlHelper urlHelper = null) { return new HtmlString(string.Join("", Environment.GetStylePaths() - .Select(stylePath => $""))); + .Select(stylePath => $""))); } private static IHtmlString RenderToString(Action withWriter)