Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use WebClient.DownloadData(to a local DummyPage.aspx)

I'm following a tutorial on this link http://www.codeproject.com/KB/aspnet/ASPNETService.aspx

Now I'm stuck at these codes

private const string DummyPageUrl = 
    "http://localhost/TestCacheTimeout/WebForm1.aspx";

private void HitPage()
{
    WebClient client = new WebClient();
    client.DownloadData(DummyPageUrl);
}

My local application address has a port number after "localhost", so how can I get the full path (can it be done in Application_Start method)? I want it to be very generic so that it can work in any cases.

Thanks a lot!

UPDATE

I tried this in the Application_Start and it runs fine, but return error right away when published to IIS7

String path = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/");
like image 598
Leo Avatar asked Nov 12 '10 10:11

Leo


2 Answers

If it is calling back to the same server, perhaps use the Request object:

var url = new Uri(Request.Url, "/TestCacheTimeout/WebForm1.aspx").AbsoluteUri;

Otherwise, store the other server's details in a config file or the database, and just give it the right value.

But a better question would be: why would you talk via http to yourself? Why not just call a class method? Personally I'd be using an external scheduled job to do this.

like image 93
Marc Gravell Avatar answered Oct 15 '22 18:10

Marc Gravell


You need an answer that works when you roll out to a different environment that maybe has a virtual application folder.

// r is Request.Url
var url = new Uri(r, System.Web.VirtualPathUtility.ToAbsolute("~/Folder/folder/page.aspx")).AbsoluteUri;

This will work in all cases and no nasty surprises when you deploy.

like image 38
MiloTheGreat Avatar answered Oct 15 '22 18:10

MiloTheGreat