Skip to content

Commit

Permalink
Templates of generic controllers are now pre-compiled
Browse files Browse the repository at this point in the history
- Improved readability of RemoveSuffix method
  • Loading branch information
bounav committed Feb 20, 2024
1 parent 30f3496 commit 4543161
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/Spark.Web.Mvc/SparkViewFactory.cs
Original file line number Diff line number Diff line change
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 4543161

Please sign in to comment.