Struggling to find an answer to my question, as I'm not exactly sure what 'type' a razor tag is.
Essentially I want to create a helper that does something along these lines:
public static xxxxxxx ScriptTag(this HtmlHelper htmlHelper, string url)
{
return @<script type="text/javascript" src="@Url.Content("~/" + url)" />;
}
The reason I want this is that I am implementing the extension methods that are outlined in this post.
Basically rather than have to do:
@Html.Resource(@<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>, "js")`
I want to be able to do:
@Html.Resource(Html.ScriptTag("Scripts/jquery-1.4.4.min.js"), "js");
Am I reaching beyond the stars here or is this possible?
Chris
When the result of a function is plain HTML, I like to use Razor Helpers.
Within your view file, create a statement like that:
then the usage is like
Razor Helpers return the HTML within the
I don't quite see the usage of the second parameter in your
ScriptTag
helper (js
). The helper is called ScriptTag so it is pretty obvious that it will render a script. Also why do you need those 2 nested helpers like that when you can directly have:But anyway if you needed such syntax you could use Razor Templated Delegates:
As we can see the
Resource
extension method in this case doesn't bring much value.You can solve this by creating an extension method for the HtmlHelper type.
For example:
You can then use your custom Html helper method as follows: