views:

69

answers:

6

Hello everybody,

after long time of searching for the solution I give up and ask this question to you.

Is it possible to read an .aspx-file from the filesystem, probably translate it into the Page-object and then render it into HTML? And then writing it back to the filesystem as a .html file.

The .aspx file on the filesystem is without the codebehind file. If possible provide me some example code.

Thanks in advance,

p.re

A: 

ASPX files are dynamic => generated HTML depends on state of the application.
If you are missing the codebehind file, you cannot properly translate the code.

Mono Project has a code evaluator. That said, it won't help you without application state.

The only thing you can do is parse the aspx file as xml (if it is valid) and filter out the dynamic content.

Jaroslav Jandek
Thats true if the ASPX file depends on the code behind, if there is no code in the code behind, you could translate the file, in theory
phsr
I thought he is missing the codebehind code.It depends on situation - whether the page itself requires application state or not.Anyway, it is pretty easy putting an aspx file to a web application and downloading resulting html.
Jaroslav Jandek
+1  A: 

This is what ASP.NET does all the time. It looks for an ASPX page on the file system, compiles it, if required, and then processes the request.

Codebehind is optional. You can have a website with only ASPX in it, without any precompiled code.

Here's a ASPX page without codebehind

<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
    <title>ClearCache</title>
</HEAD>
<body>
    <form id="ClearCache" method="post" runat="server">
        <% 
        IList keys = new ArrayList(Cache.Count);
        foreach (DictionaryEntry de in Cache) 
            keys.Add(de.Key);

        foreach (string key in keys)
        {
            this.Response.Write(key + "<br>");
            Cache.Remove(key);

        }

    %>
    </form>
</body>
</HTML>

Downloading the file as html:

var wc = new WebClient();
wc.DownloadFile(myUrl, filename);

If you don't have a ASP.NET web-server, you have to start a server. Cassini is great for this. Then your code should look like this:

var server = new Server(80,"/", pathToWebSite);
server.Start();
var wc = new WebClient();
wc.DownloadFile(server.RootUrl + "myPage.aspx", filename);
server.Stop();

If you run this more than once, the server should be cached.

Note that you could also use a RuntimeHost as mentioned by code4life. Cassini does something similar. I'd give goth a try and see, what better fits your purpose.

Michael Stoll
This example doesn't help, because it's only writing the aspx file to another file without rendering
p.re
That's only true, if you don't have a ASP.NET enabled server. I'll update my post.
Michael Stoll
+1  A: 

from remote url

byte[] buf = new byte[8192];
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(path);
webRequest.KeepAlive = false;
string content = string.Empty;
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
if (!(webResponse.StatusCode == HttpStatusCode.OK))
    if (_log.IsErrorEnabled) _log.Error(string.Format("Url {0} not found", path));

Stream resStream = webResponse.GetResponseStream();

int count = 0;
do
{
    count = resStream.Read(buf, 0, buf.Length);
    if (count != 0)
    {
        content += encoding.GetString(buf, 0, count);
    }
}
while (count > 0);

from network or virtual path

string content = string.Empty;
path = HttpContext.Current.Server.MapPath(path);

if (!File.Exists(path))
if (_log.IsErrorEnabled) _log.Error(string.Format("file {0} not found", path));

StreamReader sr = new StreamReader(path, encoding);
content = sr.ReadToEnd();
zaragon
I think he wants the rendered html (what is sent to the browser after it has finished rendering)...
Matthew Abbott
uhm ok, you're right
zaragon
A: 

I don't think you can do what you need to, without the ASP.NET runtime. If you have the ASP.NET runtime, and still want to be able to generate a HTML file from the content of an ASPX file, you could write an IHttpModule which writes the response text to a file.

Matthew Abbott
A: 

If I'm understanding your question correctly, you want an instance of the Page class created (i.e. the aspx page is compiled) and ulimately the resulting html? But you want that to happen outside the context of a web server request?

If you're looking for the html after an aspx page is actually processed, why not just grab the html returned after a page is actually rendered via IIS or whatever?

Perhaps if you shared your motivation(s) for attempting this you'll get some solid suggestions...

alex
+1  A: 

You need to use the wwAspRuntimeHost class.

Rick Strahl had a post on this, and I actually used the same approach he recommendsd to host ASP.NET runtime engine in a non-IIS environment. Here's the link:

http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp

(update to the original post)
http://www.west-wind.com/Weblog/posts/1197.aspx

code4life