Link to home
Start Free TrialLog in
Avatar of mugsey
mugseyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to get URL of website via code

I have an asp.net listview that lists a set of images and underneath the image I want to display the full URL of the image.

So like  www.testsite.com/image22.jpg

How can I contacentate

Request.Url.GetLeftPart(UriPartial.Authority)  in code behind  AND CONCATENATE IT TO  <img src="<%#Eval("picPath") %>"  
<img src="<%#Eval("picPath") %>"

Open in new window

Avatar of guru_sami
guru_sami
Flag of United States of America image

you can use asp.net image control instead of html img and this will take care of image path mapping

<asp:Image ID="Image1" runat="server"  ImageUrl='<%# "~/" +Eval("picPath") %>'/>

Avatar of mugsey

ASKER

Hi thanks but I can display the images OK however

 I need to display the full path in text

so something like

TEXT='<%# "~/" +Eval("picPath") %>'/>


Try this function:
 protected string GetFullPath(string relativepath)
    {
         //string relativepath = "pic1.jpg";
        string domainpath = Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.Port != 80 ? ":" + Request.Url.Port : "");
        string applicationpath = Request.ApplicationPath;
       
        string fullpath = domainpath + applicationpath + "/" + relativepath;
        return fullpath;
    }

Note: If your relative path already has / in front then remove "/" from full path.

Ref: http://stackoverflow.com/questions/61817/whats-the-best-method-in-asp-net-to-obtain-the-current-domain
Avatar of mugsey

ASKER

thanks sami

How can add GetFullPath  to markup?

So

TEXT='<%# "Eval("GetFullPath")  +Eval("picPath") %>'/>
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mugsey

ASKER

Guru Sami

Excellent answer!!!!!!!   ;-)