Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WebRequest & relative paths

240 views
Skip to first unread message

SpaceMarine

unread,
Mar 17, 2009, 1:51:44 PM3/17/09
to
did something change in the WebRequest object from .NET 1.1 to 2.0?

i swear i used to be able to pass relative URLs into the WebRequest
method, like so:

string relativePath = "/foo/bar.html";

WebRequest request = HttpWebRequest.Create(relativePath);
WebResponse response = request.GetResponse();

...now that generates an exception:

"Invalid URI: The format of the URI could not be determined."


so then i figured out how to create a full URL path ("http://...")
like so:

string relativePath = "/foo/bar.html";

//get the main url
string authority =
System.Web.HttpContext.Current.Request.Url.GetLeftPart
(UriPartial.Authority);

//add the supplied relative path
Uri uri = new Uri(authority + relativePath);

WebRequest request = HttpWebRequest.Create(uri.AbsoluteUri);
WebResponse response = request.GetResponse();


...that works, but im still wondering -- did this change?

thanks!
sm

gabrielgl

unread,
Feb 24, 2010, 6:06:08 PM2/24/10
to
Thanks SpaceMarine, very useful and the shortest solution I found for this issue, with no string manipulation.

Depending on directory structure maybe we need to insert Request.ApplicationPath:

Uri uri = new Uri(authority + Request.ApplicationPath + relativePath);


Didn't test it on 1.0, but is a little bit cumbersome to make it working; I suppose it should be something similar to ResponseRedirect(~/page.aspx)...? you right...

SpaceMarine wrote:

WebRequest & relative paths
19-Mar-09

did something change in the WebRequest object from .NET 1.1 to 2.0?

i swear i used to be able to pass relative URLs into the WebRequest
method, like so:

string relativePath = "/foo/bar.html";

WebRequest request = HttpWebRequest.Create(relativePath);
WebResponse response = request.GetResponse();

....now that generates an exception:

"Invalid URI: The format of the URI could not be determined."


so then i figured out how to create a full URL path ("http://...")
like so:

string relativePath = "/foo/bar.html";

//get the main url
string authority =
System.Web.HttpContext.Current.Request.Url.GetLeftPart
(UriPartial.Authority);

//add the supplied relative path
Uri uri = new Uri(authority + relativePath);

WebRequest request = HttpWebRequest.Create(uri.AbsoluteUri);
WebResponse response = request.GetResponse();


....that works, but im still wondering -- did this change?

thanks!
sm

Previous Posts In This Thread:

On Thursday, March 19, 2009 4:32 AM
SpaceMarine wrote:

WebRequest & relative paths


did something change in the WebRequest object from .NET 1.1 to 2.0?

i swear i used to be able to pass relative URLs into the WebRequest
method, like so:

string relativePath = "/foo/bar.html";

WebRequest request = HttpWebRequest.Create(relativePath);
WebResponse response = request.GetResponse();

....now that generates an exception:

"Invalid URI: The format of the URI could not be determined."


so then i figured out how to create a full URL path ("http://...")
like so:

string relativePath = "/foo/bar.html";

//get the main url
string authority =
System.Web.HttpContext.Current.Request.Url.GetLeftPart
(UriPartial.Authority);

//add the supplied relative path
Uri uri = new Uri(authority + relativePath);

WebRequest request = HttpWebRequest.Create(uri.AbsoluteUri);
WebResponse response = request.GetResponse();


....that works, but im still wondering -- did this change?

thanks!
sm


Submitted via EggHeadCafe - Software Developer Portal of Choice
More Fun with Fluent NHibernate Automapping
http://www.eggheadcafe.com/tutorials/aspnet/50aa9259-6dbb-4d16-9639-81ee42171b00/more-fun-with-fluent-nhib.aspx

0 new messages