Reputation: 15872
What's a good method for getting the content of a .aspx web page as a String
. This is from a C# Class which is compiled and deployed to the same machine.
Should I use a HTTP request? Or is there a means of doing the same through a file path, and would this trigger the code behind of the page?
Upvotes: 0
Views: 2235
Reputation: 7961
This code retrieves the root directory that is hosting the application in IIS. Then, it concatenates the filename. Finally, it reads the contents into a string variable.
Reading a file from disk does not trigger the code-behind; only a request through the ASP.NET pipeline does that.
string path = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "Index.aspx");
string fileContents = File.ReadAllText(path);
Upvotes: 1
Reputation: 223422
If you use HTTP request, like WebClient
and use WebClient.DownloadString(@"http://someSite/somepage.aspx")
, that would trigger the server side code and you will get HTML
generated by the server. Not the actual aspx
page.
But if you use File.ReadAllLines('somepage.aspx')
from your current project then you will get the file contents and it will not trigger the server side code. But you can only do that from your current project. You can't access an aspx
page over http
Upvotes: 2
Reputation: 14972
An HTTP request would usually not return the content of the aspx file since the server should interpret it (take this with a grain of salt, it all depends on your server and how its configured, maybe you've got a special setup)
If you want to read the content of the aspx file, open the file using its location on the disk. It won't trigger the code behind the page since this is triggered by the web server and you're not using it using a file path
if you want to read the output produced when the aspx file is read, open the url to this file. It will trigger the backing code and you will received the output.
Upvotes: 0