views:

151

answers:

2

I have 2 question

  1. How to get absolute Uri in asp.net. For example: Input: "~/Files/" , Output:"www.sampleweb.com/Files/"

  2. What is the "go to root" command in url on the client side like server side command "~/Files/"

For example:

File location

myAspProjectName/Files/aa.jpg
myAspProjectName/User/bb.aspx

into the bb.aspx

<asp:image ImageUrl="~/Files/aa.jpg" id="img1" runat="server"/>

this work fine. But

<img src="~/Files/aa.jpg" alt=""/>

don't work

A: 

I don't think there is a shortcut syntax to map a physical path to a virtual one including the domain name. However, this will work:

string absoluteUri = Request.ServerVariables["SERVER_NAME"] + Request.ServerVariables["URL"];

Not sure what you mean by the second question - please can you expand?

Mark B
+2  A: 

Like Mark B says there is no obvious syntactic shortcut for this.

If you're doing it with the current request then you can just use Request.Url or for an arbitrary file you can use Page.ResolveURL("~/some/path") if you are using an ASP.Net page. These are methods that will generate absolute relative URIs i.e. those of the form /path/somefile then if you then get the host name using Mark B's approach you can concatenate the two things together.

As for (2) I think you possibly mean absolute relative URIs. If a relative URI begins with a forward slash it is resolved as being relative to the base domain e.g. if you were on the page http://yourdomain.com/somedir/somepage.aspx and you followed a link which was /otherdir/somepage.aspx you'd end up at http://yourdomain.com/otherdir/somepage.aspx

RobV