Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor @helper in App_Code cannot be accessed

In my App_code, I have a helper function called FormatTelephone(string number) in Formatter.cshtml. I tried to access it in a partial view by @Formatter.FormatTelephone(number). When I test it, it says

Compiler Error Message: CS0103: The name 'Formatter' does not exist in the current context

What is the likely cause of it? Thanks!

like image 518
uni Avatar asked Oct 20 '11 16:10

uni


2 Answers

I ran into this exact problem when deploying the site onto another server. Make sure the App_Code/Formatter.cshtml file is actually copied to the server! My mistake was the file has a build action that was set to 'None'. Right click on the file and select Properties, and then set the Build Action to 'Content'.

NOTE:

If you don't have asp.net mvc 3/4 installed make sure the following dlls are in your bin folder.

  • System.Web.Mvc
  • Microsoft.Web.Infrastructure
  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Razor

Scott Hanselman has a blog post about what might be causing your issue. BIN Deploying ASP.NET MVC 3 with Razor to a Windows Server without MVC installed

like image 152
superlogical Avatar answered Oct 08 '22 13:10

superlogical


The following works for me in ~/App_Code/Formatter.cshtml:

@helper FormatTelephone(string number)
{
    <div>Formatted @number</div>
}

and then in some view:

@Formatter.FormatTelephone("123")
like image 40
Darin Dimitrov Avatar answered Oct 08 '22 12:10

Darin Dimitrov