How can I use Url.Content inside @helper?

I am doing some @helper inside the App_Code folder. The goal is to completely replace my old HtmlHelpers, written in a * .cs file, with a string builder and all other interesting stuff.

Anyway, in several places I use Url.Content in src tag attributes <img>. They worked great as HtmlHelpers. Now that I have received my code inside the * .cshtml file in the App_Code folder, the site does not want to compile:

CS0103: the name 'Url' does not exist in the current context

What is a good way to solve this problem? I would not want to have a relative path there instead of displaying the path.

+3
source share
1 answer

You can pass it as an argument to the helper:

@helper Foo(UrlHelper url) {
    @url.Action("~/foo");
}

:

@Foo(Url)
+3

All Articles