views:

2184

answers:

5

We're building a CMS. The site will be built and managed by the users in aspx pages, but we would like to create a static site of HTML's. The way we're doing it now is with code I found here that overloads the Render method in the Aspx Page and writes the HTML string to a file. This works fine for a single page, but the thing with our CMS is that we want to automatically create a few HTML pages for a site right from the start, even before the creator has edited anything in the system. Does anyone know of any way to do this?

+2  A: 

Calling the Render method is still pretty simple. Just create an instance of your page, create a stub WebContext along with the WebRequest object, and call the Render method of the page. You are then free to do whatever you want with the results.

Alternatively, write a little curl or wget script to download and store whichever pages you want to make static.

Frank Krueger
+2  A: 

You could use wget (a command line tool) to recursively query each page and save them to html files. It would update all necessary links in the resulting html to reference .html files instead of .aspx. This way, you can code all your site as if you were using server-generated pages (easier to test), and then convert it to static pages.

If you need static HTML for performance reasons only, my preference would be to use ASP.Net output caching.

ckarras
+2  A: 

I recommend you do this a very simple way and don't do it in code. It will allow your CMS code to do what the CMS code should do and will keep it as simple as possible.

Use a product such as HTTrack. It calls itself a "website copier". It crawls a site and creates html output. It is fast and free. You can just have it run at whatever frequency you think is best.

It decouples your HTML output needs from your CMS design and implementation. It reduces complexity and gives you some flexibility in how you output the HTML without introducing failure points in your CMS code.

jttraino
+1  A: 

@ckarras: I would rather not use an external tool, because I want the HTML pages to be created programmatically and not manually.

@jttraino: I don't have a time interval in which the site needs to be outputted- the uotput has to occur when a user creates a new site.

@Frank Krueger: I don't really understand how to create an instance of my page using WebContext and WebRequest.

I searched for "wget" in searchdotnet, and got to a post about a .net class called WebClient. It seems to do what I want if I use the DownloadString() method - gets a string from a specific url. The problem is that because our CMS needs to be logged in to, when the method tries to reach the page it's thrown to the login page, and therefore returns the login.aspx HTML...

Any thoughts as to how I can continue from here?

Lea Cohen
+3  A: 

I seem to have found the solution for my problemby using the Server.Ecxcute method.

I found an article that demonstared the use of it:

TextWriter textWriter = new StringWriter();
Server.Execute("myOtherPage.aspx", textWriter);

Then I do a few maniulatons on the textWriter, and insert it into an html file. Et voila! It works!

Lea Cohen