Robert
Robert

Reputation: 169

C#.NET find actual url from page alias

If I have a URL like www.mydomain.co.uk/shopping

Is it possible to find the real URL of shopping?

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("www.mydomain.co.uk/shopping");

I quickwatched 'req' but couldn't see it?

anyone got any ideas?

Upvotes: 0

Views: 1193

Answers (3)

Jon Hanna
Jon Hanna

Reputation: 113282

If you want to obtain the response anyway, and to also know the redirected-to URI, then use HttpWebRequest as normal, and examine the ResponseUri property of the HttpWebResponse.

If you want to obtain the redirected-to-URI without following the redirect, then set the AllowAutoRedirect of the HttpWebRequest to false, and examine the "Location" header of the HttpWebResponse.

In the later case, it's always possible that there is a subsequent redirect. If you need to know this, then you're probably better of going back to the first method (which will follow the chain of redirects). If you need to know all the redirects you will have to follow the chain yourself. Be careful to limit the total number you will follow to avoid falling into a redirect blackhole (catching duplicates is also a good idea, but you still need an upper limit as blackholes can exist without duplication).

Upvotes: 2

Vinay B R
Vinay B R

Reputation: 8421

try this -

Uri ourUri = new Uri(url);                    
WebRequest myWebRequest = WebRequest.Create(url);         
WebResponse myWebResponse = myWebRequest.GetResponse();         
string actualUri = myWebResponse.ResponseUri;

Upvotes: 0

Oded
Oded

Reputation: 499062

I am assuming you are talking about server URL rewriters, where a URL like this:

http://example.com/product/view/2

Is in fact handled by:

http://example.com/product.aspx?id=2

If the server is doing internal re-routing (such as with MVC routes in IIS), then no, this is not possible, as it is fully internal and the actual page/handler URL does not get exposed to the client.

Upvotes: 1

Related Questions