0

I have some text which is loaded from a resource file. Ordinarily, to get dynamic content I would use:

string.Format(GetLocalResourceObject("SomeText"), PhoneNumberAsString) 

I want to do the same with a link, only the link needs to be application relative as I have URLs like mysite.com/page.aspx and mysite.com/fr/page.aspx.

I normally use an <asp:HyperLink /> tag to create the links as I can then just put a squiggle at the start NavigateUrl="~/page.aspx". However, I don't know of a way to get a dynamic HyperLink to appear as a string without adding it as a control to something.

Simply writing ToString() outputs System.Web.UI.WebControls.HyperLink..

How do I get a link out of a resource file and make it into a hyperlink using ASP.NET Webforms?

UPDATE

With some help from the answers I now have the following code on my page:

<p><%= string.Format(GetGlobalResourceObject("Resource", "MoreThan1000Users").ToString(), ResolveUrl("~/contact-us.aspx")) %></p>

and in my resource file I have:

If you would like more than 1000 users please <a href="{0}">call our sales team</a>.

Does this seem like good practice or is there another way to achieve what I'm doing? I don't know if I should be happy or not that there is HTML inside the resource file.

Chris
  • 2,630
  • 3
  • 31
  • 58

2 Answers2

1

Since you haven't posted code, I'm guessing somewhere you have a HyperLink WebControl object that you're hitting ToString() on. If that's the case, you can access the URL associated with it using its myHyperLinkControl.NavigateUrl property.

If you're storing the link in your resource with a squiggle/tilde (which is good) then you can replace the squiggle with your application location. If you have a control/page, then you can easily call its ResolveURL method (which takes the tilde and automatically replaces it) There's some existing solutions if you don't have a control/page reference with your context, then there's some discussion to do that here: ResolveUrl without an ASP.NET Page

Community
  • 1
  • 1
Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
  • You've definitely got me a solution, the issue now is I'm questioning my practices. Please see my update for my code. I suppose my issue is whether I should have the link HTML within the resource file. – Chris May 04 '12 at 13:43
  • Based on your edit, I'm not sure whether or not that should be a separate question. Honestly, I haven't touched ASP.NET for a little while now so I'm not sure the best way to handle this, especially if your link is part of the text. Perhaps you can have 3 separate resources (before-link-text, contact-us-link-URL, link-text) and you can put the link HTML in your markup. You could have multiple references to the Contact-Us URL shared via the reference. But maybe your best bet is to have that as a new question so more experienced people can find and answer it. – Chris Sinclair May 04 '12 at 14:05
  • Ahh, I see you've edited quite a bit of the question. Hopefully there's a good solid practice for this. I know if I was making it, I'd probably straight up have separate controls handling the appearance/markup and leave resources for filling in the text content or shared URLs. – Chris Sinclair May 04 '12 at 14:08
  • Haha I totally had that three resources thing you mentioned!! Then I decided it looked too messy so I ripped it out and stuck the HTML in the resource! But yes you're right it's more of a resources-best-practice question so I will ask a new question for my answer to that, thank you for your help! – Chris May 04 '12 at 15:44
0

I guess this is what you want:

Server.MapPath("~/page.aspx")

That will work inside your aspx and your code-behind.

Adauto
  • 569
  • 3
  • 5