Having razor @helper in App_code folder using ASP.

2019-04-03 07:55发布

问题:

Where can I find tutorials on how exactly get this to work?

I am trying to have @helper in App_code directory of my MVC3 app, so far when I try to use them in other razor pages, they cannot be found.

回答1:

You need to call the helpers as static methods of the page name.

For example, if you have a helper named HelpMe in App_Code/MyHelpers.cshtml, you would write

@MyHelpers.HelpMe(...)


回答2:

Scott Gu just wrote a blog post about this:

http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx



回答3:

Look at this SO question and associated answer: Razor: Declarative HTML helpers

It does work, though I find that in the end, I usually opt for writing another extension method off of HtmlHelper.