Unable to access Razor @helper in App_Code

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

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

What is the likely reason for this? Thanks!

+7
source share
2 answers

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

Note:

If you do not 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 cause your problem. BIN Deploying ASP.NET MVC 3 Using Razor on Windows Server Without Installing MVC

+7
source

In ~/App_Code/Formatter.cshtml following works for me:

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

and then in some cases:

 @Formatter.FormatTelephone("123") 
+1
source

All Articles