I have an .aspx page in a project. There is another class library project in the same solution which runs as a batch. I want to call this aspx page from this class library, get its html page, convert it to pdf and save it on the drive. what is the best way to make the http request and use the response. I am new to asp.net. Please help!
Asked
Active
Viewed 587 times
1 Answers
0
The easiest way to do this is by instantiating a new instance of System.Net.WebClient
and it like so:
var client = new WebClient();
var html = client.DownloadString("http://www.your-url-goes-here.com");
Alternatively (depending on your .NET version) you can also use the new HttpClient
from System.Net.Http
(you might want to get this package from NuGet):
var client = new HttpClient();
var html = await client.GetStringAsync("http://www.your-url-goes-here.com");
In both cases, the html
variable will contain the generated mark-up of the web page.
A better way would be to determine upfront what you need to render and direct your PDF generator to create a page like that. This would save you from the web server roundtrip that gives you the generated HTML.

Alex
- 391
- 2
- 9
-
Please bear with me if my question sounds too lame. How do I determine the URL? Should my aspx page be hosted on a server to be called? – user1860689 Dec 08 '14 at 13:31
-
If you have an .aspx page it is typically called by web browsers, and thus hosted on a web server. If you want to use that approach, you'll need to host the .aspx page on a web server. You'll need to work with a server administrator to figure out the exact URL to your page, as I don't know your environment. – Alex Dec 08 '14 at 13:38