Skip to content

Commit

Permalink
Merge pull request #78 from SparkViewEngine/precompilation-not-locati…
Browse files Browse the repository at this point in the history
…ng-views-of-generic-controllers

Templates of generic controllers are now pre-compiled
  • Loading branch information
RobertTheGrey committed Feb 25, 2024
2 parents 30f3496 + 4543161 commit e04fa77
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/Spark.Web.Mvc/SparkViewFactory.cs
Expand Up @@ -276,7 +276,22 @@ public IList<SparkViewDescriptor> CreateDescriptors(SparkBatchEntry entry)
{
var descriptors = new List<SparkViewDescriptor>();

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<string>();

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e04fa77

Please sign in to comment.