In an ASP.MVC application, I have a function defined in my _ViewStart.cshtml like this:
@functions
{
public void Foo()
{
....
}
}
When I try to use this function in of my views, I get an exception saying that :
The name ‘Foo’ does not exist in the current context
I thought that all what I define in _ViewStart.cshtml is accessible to all my views that define it as layout.
I am missing something here …
Thank you for your help
You could place reusable functions them inside Razor views of the special
App_Codefolder.For example inside
~/App_Code/Foo.cshtmlyou could declare aBarfunction:that will be accessible from any Razor view:
Also notice that the function must be declared as static.