Asp.Net MVC @helper template in different folder t

2019-07-20 04:19发布

问题:

This question already has an answer here:

  • Can I have a global razor @helper outside of App_Code? 4 answers

Like I said in the title of my question, is it possible to put the @helper template of cshtml file in a folder other than App_Code to be visible by my views?

I discovered the very nice article from ScottGu but I don't found any solution to my question.

Thank you.

回答1:

You can, if you write them as extension methods (static methods of a static class). Something like:

public static string AMethod(this HtmlHelper html, int arg)
...

Lets you call the method in a cshtml file as

@Html.AMethod(5)