From 4543161a69bd4d7909e5f16db7c7deb30efad960 Mon Sep 17 00:00:00 2001 From: bounav Date: Tue, 20 Feb 2024 10:26:33 +0000 Subject: [PATCH] Templates of generic controllers are now pre-compiled - Improved readability of RemoveSuffix method --- src/Spark.Web.Mvc/SparkViewFactory.cs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Spark.Web.Mvc/SparkViewFactory.cs b/src/Spark.Web.Mvc/SparkViewFactory.cs index 1ac07f1d..3039a0c3 100644 --- a/src/Spark.Web.Mvc/SparkViewFactory.cs +++ b/src/Spark.Web.Mvc/SparkViewFactory.cs @@ -276,7 +276,22 @@ public IList CreateDescriptors(SparkBatchEntry entry) { var descriptors = new List(); - var controllerName = RemoveSuffix(entry.ControllerType.Name, "Controller"); + string controllerName = null; + + if (entry.ControllerType.ContainsGenericParameters) + { + // generic controller have a backtick suffix in their (name e.g. "SomeController`2") + var indexOfBacktick = entry.ControllerType.Name.IndexOf("Controller`", StringComparison.Ordinal); + if (indexOfBacktick > -1) + { + // removing it otherwise locating the view templates will fail + controllerName = entry.ControllerType.Name.Substring(0, indexOfBacktick); + } + } + else + { + controllerName = RemoveSuffix(entry.ControllerType.Name, "Controller"); + } var viewNames = new List(); @@ -375,12 +390,9 @@ private static bool TestMatch(string potentialMatch, string pattern) private static string RemoveSuffix(string value, string suffix) { - if (value.EndsWith(suffix, StringComparison.InvariantCultureIgnoreCase)) - { - return value.Substring(0, value.Length - suffix.Length); - } - - return value; + return value.EndsWith(suffix, StringComparison.InvariantCultureIgnoreCase) + ? value.Substring(0, value.Length - suffix.Length) + : value; } #region IViewEngine Members