24

I'm trying to do this:

<a href="~/Cases/SupRequestSearch.aspx">Search request</a>

so I need the ~ to be rendered as http://myserver/app/...

in mvc I would do

<a href="<%=Url.Content("~/Cases/SupRequestSearch.aspx")%>>Search request</a>

is there something similar in asp.net web forms ?

Francis Gilbert
  • 3,382
  • 2
  • 22
  • 27
Omu
  • 69,856
  • 92
  • 277
  • 407
  • 3
    Try Using Page.ResolveUrl in Webforms Also answered Here : [Here][1] [1]: http://stackoverflow.com/questions/1630175/url-content-in-asp-net –  Sep 02 '11 at 13:48

5 Answers5

59

As rapadai mentioned above, the equivalent of

Url.Content("~/path/to/file.ext") // MVC

in webforms is

Page.ResolveUrl("~/path/to/file.ext") // Webforms
Ben Cull
  • 9,434
  • 7
  • 43
  • 38
12

Try adding runat="server" to your tag.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
5

Try this:

<asp:hyperlink  id="Search" NavigateUrl="~/Cases/SupRequestSearch.aspx" runat="server" />

or just

<a href="~/Cases/SupRequestSearch.aspx" id="Search" runat="server">Search request</a>
Francis Gilbert
  • 3,382
  • 2
  • 22
  • 27
1

If you don't have either Url or Page you can still use

VirtualPathUtility.ToAppRelative(string) or VirtualPathUtility.ToAbsolute(string)

You still need to be within a web context of course - or this doesn't make sense.

See also : ResolveUrl without an ASP.NET Page

Community
  • 1
  • 1
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
0
<%= Page.ResolveUrl("~/Path/To/Page") %>
Calvin Fisher
  • 4,653
  • 5
  • 36
  • 47
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – gunr2171 May 08 '15 at 18:02
  • @gunr2171 The most-upvoted answer in that post you provided says: "Everyone is free to up-vote whatever sort of answer they find useful. I'm not particularly fond of code-only answers either, `but for certain questions they are enough`." The accepted answer says "Then again; `often a short snippet of code really is all that is needed. I've seen some answers that went on and on and on with explanations of stuff that doesn't so much matter`" In this case, the question just asked, "what is the command to do X?" I posted the command. Is my answer in some way incorrect, that you downvoted it? – Calvin Fisher May 12 '15 at 22:11