How can I use Url.Content inside of a @helper?
I'm making some @helper's inside of the App_Code folder. The intent is to completely replace my old HtmlHelpers written in a *.cs file with a string builder and all the other fun stuff.
Anyway, in a couple of places, I'm using Url.开发者_运维技巧Content in src attributes of <img>
tags. These used to work fine as HtmlHelpers. Now, that I got my code inside *.cshtml file in App_Code folder, the site doesn't want to compile:
CS0103: The name 'Url' does not exist in the current context
What's a good way to solve this? I wouldn't want to have a relative path there instead of path mapping.
You could pass it as argument to the helper:
@helper Foo(UrlHelper url) {
@url.Action("~/foo");
}
and then:
@Foo(Url)
精彩评论