Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor: Declarative HTML helpers

I'm trying to write a simple declarative html helper:

@helper Echo(string input) {     @input } 

The helper works fine if I embed it into the page I want to use it on. But if I move it to a separate .cshtml file and place that file in the ~/Views/Helpers directory, my view can't be compiled anymore because the helper is not found. According to Scott Gu's blog article on Razor it should work.

What am I doing wrong?

like image 342
Adrian Grigore Avatar asked Dec 15 '10 15:12

Adrian Grigore


People also ask

Which Razor syntax is an HTML helper?

Inline HTML Helper is used to create a reusable Helper method by using the Razor @helper tag. Inline helpers can be reused only on the same view.

Is Cshtml a Razor?

cshtml files are razorpages or MVC views, they does not contain any C#-written client-side code. If you wan to do so, you must use JavaScript. However, a . razor file, also know as a Razor component, can have C# written in it and run on client's browser.

Should I use tag helpers or HTML helpers?

Tag Helpers are attached to HTML elements inside your Razor views and can help you write markup that is both cleaner and easier to read than the traditional HTML Helpers. HTML Helpers, on the other hand, are invoked as methods that are mixed with HTML inside your Razor views.


1 Answers

The ~/Views/Helpers location as describe in that ScottGu post will not work in MVC 3 RTM. At the time that article was written it was a design idea we were tossing around but ultimately we were not able to implement it.

Putting your helpers in App_Code works but has certain limitations that impact certain MVC scenarios (for example: no access to standard MVC Html. helpers)

like image 141
marcind Avatar answered Sep 17 '22 15:09

marcind