arame3333
arame3333

Reputation: 10193

C# I want the url, not the physical pathname

For this line of code;

string link = HttpContext.Current.Server.MapPath("/Contract/Details/" + this.ContractId.ToString());

I get the physical pathname on C drive.

What I want is the url, ie

http://localhost:1234/Contract/Details/1

How do I get this?

Upvotes: 1

Views: 199

Answers (4)

Antonio Bakula
Antonio Bakula

Reputation: 20693

Uri base = new Uri("http://localhost:1234/";);

Uri file = new Uri(host, "/Contract/Details/" + this.ContractId.ToString());

string URL = file.AbsoluteUri;

Upvotes: 0

Tim Robinson
Tim Robinson

Reputation: 54734

// Use the Uri constructor to form a URL relative to the current page
Uri linkUri = new Uri(HttpContext.Current.Request.Url, "/Contract/Details/" + this.ContractId.ToString());
string link = linkUri.ToString();

Upvotes: 4

NinjaCat
NinjaCat

Reputation: 10194

There's a great article on .Net paths @ http://west-wind.com/weblog/posts/132081.aspx

Take a look at the Url or PathInfo property.

Upvotes: 3

anishMarokey
anishMarokey

Reputation: 11397

try this:

string url = HttpContext.Current.Request.Url.AbsoluteUri;

Upvotes: 4

Related Questions