ResolveUrl method is define in System.Web.UI.Control which is indirectly inherited by WebForm View.
However Razor View is inherited from WebViewPage which is inherited from WebPageBase which is inherited from WebPageRenderingBase which is inherited from WebPageUltimateBase which is the base class for all CSHTML files. Just see the MVC 3 code here
http://aspnet.codeplex.com/releases/view/50092
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by shapper on Aug 22, 2010 10:54 PM
shapper
Contributor
3932 Points
3789 Posts
Razor and ResolveUrl
Aug 22, 2010 02:09 AM|LINK
Hello,
I have the following:
<script type="text/javascript"> var root = '<%=ResolveUrl("~/")%>'; </script>And I am trying to do the same with Razor:
<script type="text/javascript"> var root = '@ResolveUrl("~/")'; </script>But I keep having the error:
Compiler Error Message: CS0103: The name 'ResolveUrl' does not exist in the current context
What am I doing wrong?
Thanks,
Miguel
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><script type="text/javascript"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> var root = '<%=ResolveUrl("~/")%>'; </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></script></div>HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: Razor and ResolveUrl
Aug 22, 2010 04:52 AM|LINK
Try @{ResolveUrl(...)} or @{ResolveUrl(...);}
Note: Untested.
blog: www.heartysoft.com
twitter: @ashic
tanatrajan
Participant
1784 Points
370 Posts
Re: Razor and ResolveUrl
Aug 22, 2010 07:59 AM|LINK
use the syntax below
<link type="text/css" href="@Href("~/Styles/jquery-ui-1.8.4.custom.css")" rel="stylesheet" />
<link type="text/css" href="@Href("~/Styles/StyleSheet.css")" rel="stylesheet" />
<script type="text/javascript" src="@Href("~/Scripts/jquery-1.4.2.min.js")"></script>
<script type="text/javascript" src="@Href("~/Scripts/jquery-ui-1.8.4.custom.min.js")"></script>
shapper
Contributor
3932 Points
3789 Posts
Re: Razor and ResolveUrl
Aug 22, 2010 01:23 PM|LINK
I tried both and didn't work ... Any idea?
shapper
Contributor
3932 Points
3789 Posts
Re: Razor and ResolveUrl
Aug 22, 2010 01:25 PM|LINK
I also tried with Href and have the same problem.
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Razor and ResolveUrl
Aug 22, 2010 03:32 PM|LINK
Try, Url.Content("~/")
ResolveUrl method is define in System.Web.UI.Control which is indirectly inherited by WebForm View.
However Razor View is inherited from WebViewPage which is inherited from WebPageBase which is inherited from WebPageRenderingBase which is inherited from WebPageUltimateBase which is the base class for all CSHTML files. Just see the MVC 3 code here
http://aspnet.codeplex.com/releases/view/50092
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
shapper
Contributor
3932 Points
3789 Posts
Re: Razor and ResolveUrl
Aug 22, 2010 10:54 PM|LINK
This worked just fine:
<script type="text/javascript"> var root = '@Url.Content("~/")'; </script>Thanks,
Miguel
mrslowly
Member
63 Points
20 Posts
Re: Razor and ResolveUrl
May 25, 2011 07:30 PM|LINK
In MVC3 project, I create HtmlHelperExtension for ResolveUrl.
it work fine in my test. and here is code:
namespace System.Web.Mvc.Html { public static class HtmlHelperExtensions { public static string ResolveUrl(this HtmlHelper helper,string relativeUrl) { if (VirtualPathUtility.IsAppRelative(relativeUrl)) { return VirtualPathUtility.ToAbsolute(relativeUrl); } else { var curPath = WebPageContext.Current.Page.TemplateInfo.VirtualPath; var curDir = VirtualPathUtility.GetDirectory(curPath); return VirtualPathUtility.ToAbsolute(VirtualPathUtility.Combine(curDir, relativeUrl)); } } } }For test in cshtml file:
@Html.ResolveUrl("images") @Html.ResolveUrl("../images")