Given the following string extension method
namespace JHS.ExtensionMethods
{
public static class StringExtensions
{
public static string ToUSAPhone(this String str)
{
return String.Format("{0:(###) ###-####}", Double.Parse(str));
}
}
}
A @using statement was added to the MVC4 Razor view
@using JHS.ExtensionMethods;
and the following string value calls the extension method
@Model.producer.phone.ToUSAPhone()
which results in the following error
'string' does not contain a definition for 'ToUSAPhone'
I also tried putting the namespace in the web.config of the /Views folder and receive the same error.
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="JHS.ExtensionMethods"/>
</namespaces>
</pages>
I have verified the extension method works by putting the same call in a C# class
string test=producer.phone.ToUSAPhone();
It seems the reference to the extension method is not available in the MVC4 Razor view but I can't figure out why?
It's not just if the type you're calling the extension method on is dynamic, but if anything in the expression is dynamic and not cast.
eg this is clearly dynamic:
But I first thought Mike's answer did not apply to me because I was doing this :
where
ToJSON()
is my extension method andGetJSONProfile()
just returnsobject
.I was just spacing out and being stupid but wanted to mention this.
There may be yet another trivial reason for this and it happened to me.
The file that I created my extension in had "Content" as value for Build Action property on VS file properties pane.
Switching it to "Compile" immediately fixed the issue, naturally...
This happens if the type you are trying to use the extension method on is actually a
dynamic
. Check to see if the exception is being generated by the CSharp RuntimeBinder. If so, you can either use the method as a common or garden static method:Or you can cast the value to a string:
According to Eric Lippert (formerly of MSFT):
Build your project before adding your custom namespace for the extentions to your View.