Unable to get Razor helpers (@helper) working (via App_Code or PageStart) RRS feed

  • Question

  • User-1206726903 posted

    I'm building a small prototype app in Facebook's React; and I'm using WebPages so I can easily share some code/templates but don't need all of the bloat that comes with MVC.

    I'm trying to define some small helper functions that I can use in any cshtml page; but I'm struggling to use them from anything other than the cshtml file I declared them in.

    I have tried both defining them in PageStart.cshtml and in App_code/blah.cshtml. I've tried calling them as @Helpers.blah and just @blah. Neither of these "compile" (red quigglies in VS) nor work at runtime ("The name 'blah' does not exist in the current context").

    I even checked in the assembly using ildasm, and couldn't find any trace of my helper when in App_Code!

    Saturday, May 31, 2014 11:36 AM


All replies

  • User1641955678 posted

    If it's in App_Code/Blah.cshtml and your method is called Foo, you need to call it as Blah.Foo. e.g. see http://stackoverflow.com/questions/4866462/having-razor-helper-in-app-code-folder-using-asp-net-mvc-3

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 31, 2014 11:46 AM
  • User-1206726903 posted

    Ugh; doh! This works fine! :) 

    I must've misread the page I found; thought it said it was Helpers.[filename] (which left me wondering where the helper name went!)

    Does it have to be in App_Code, or can I call them from other locations?

    Saturday, May 31, 2014 11:51 AM
  • User1641955678 posted

    The helper needs to be in App_Code, and you can call it from other places in App_Code or from random other .cshtml files.

    Saturday, May 31, 2014 5:10 PM