RazorLight
RazorLight copied to clipboard
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'string' does not contain a definition for 'Pluralize'
Describe the bug Extension method not working as expected.
To Reproduce
[Fact]
public async Task Should_Compile_Template()
{
var engine = new RazorLightEngineBuilder()
.UseMemoryCachingProvider()
.Build();
var template = "@using Humanizer@(Model.Name.Pluralize())";
var component = new Component { Name = "Customer"};
var result = await engine.CompileRenderAsync("templateKey", template, component);
Assert.That(result, Is.EqualTo(component.Name.Pluralize()));
}
Expected behavior Should resolve to 'Customers' but it does not
Information (please complete the following information):
- OS: [ Windows 10 pro ]
- Platform [.NET Core 2.1]
- RazorLight version [ 2.0-beta1 ]
Additional context However explicit use of an extensions method passes as expected:
[Fact]
public async Task Should_Compile_Template()
{
var engine = new RazorLightEngineBuilder()
.UseMemoryCachingProvider()
.Build();
var template = "@using Humanizer@(InflectorExtensions.Pluralize(Model.Name))";
var component = new Component { Name = "Customer" };
var result = await engine.CompileRenderAsync("templateKey", template, component);
Assert.That(result, Is.EqualTo(InflectorExtensions.Pluralize(component.Name)));
}
I have wrapped an extension method around the humanizer Pluralize() which was defined in the executing assembly, even that does not work, so its definitely extension methods;
i'm also facing the same issue.
@wendellmva Have you tried something like this yet?
var engine = new RazorLightEngineBuilder()
.AddDefaultNamespaces("Humanizer")
.UseMemoryCachingProvider()
.Build();
EDIT:
Make sure you also have a model to your Component
class, otherwize you might have some issues:
var template = "@model Your.Namespace.Component" + Environment.NewLine +
"Model.Name.Pluralize()";
Marked as a docs issue based on @bleutwo feedback. Thank you, Sven. I will look at the regression suite to see if this behavior is defined.