25

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 ?

1

5 Answers 5

60

As rapadai mentioned above, the equivalent of

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

in webforms is

Page.ResolveUrl("~/path/to/file.ext") // Webforms
Sign up to request clarification or add additional context in comments.

Comments

12

Try adding runat="server" to your tag.

5 Comments

because, how is runat="server" going to help him with his question?
@AlvinfromDiaspar - it creates a server tag that then asp.net can interpret and convert the url.
So that means adding the server isn't enough. there is actually more he must do to convert the url?
So this is sufficient, <a runat="server" href="<%=Url.Content("~/Cases/SupRequestSearch.aspx")%>>Search request</a> ?
So basically the Url.Content is "performed" on a url if it is processed on the server?
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>

Comments

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

Comments

0
<%= Page.ResolveUrl("~/Path/To/Page") %>

2 Comments

While this code snippet may solve the question, including an explanation 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 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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.