Asp.net mvc html melper
Ive got this function as a html helper which should write a javascript function onto the page, I then call this in the masterpage like this <%Html.RenderBaseUrlScript(); %>
My problem is that the script never seems to be written to 开发者_高级运维the page, I search in the source and cannot see it anywhere, I have tried moving this around from the head to the body of the html masterpage, Im really confused as to why this is not workung, please help public static string RenderBaseUrlScript(this HtmlHelper helper) {
return string.Format("<script type='text/javascript'> $.url=function(url){{return '{0}'+url;}}", GetBaseUri());
}
Try:
<%= Html.RenderBaseUrlScript() %>
Notice the = sign and no semicolon. Since your html helper method is not writing anything directly to the response stream (which is good), you need to actually call Response.Write()
on the output of your function. The <%= %>
tags are short form for Response.Write()
.
If your helper method was writing directly to the response stream itself, your code would be working. But it's much better to have it this way.
精彩评论