Forum Moderators: open

Message Too Old, No Replies

ASP Bag of Tricks

         

musicales

8:05 am on Apr 28, 2003 (gmt 0)

10+ Year Member



I've seen the great PHP Bag of tricks thread [webmasterworld.com] on this site. How about getting one going for ASP. I've got plenty stored up, but here's one to get started:

Create search engine friendly static pages from a dynamic page in ASP

Let's say you've got a page called

product.asp
which receives a querystring
product_id
, as in

/products/product.asp?product_id=351

In your dynamic page, you display the product based on the

product_id
you receive.

Change the

product.asp
page so the entire contents fall within a function called
show_product(product_id)
, as in

<%function show_product(product_id)
'old product.asp page including any html etc.
end function%>

Now you just need to generate a page for each product_id in the database. This is what the page will look like for
product_id
351:


<!-- #include file="product.asp"-->
<%call show_article(351)%>

We need to save this page as 351.asp

Here's how to generate a file like that for every product_id in the database. Don't forget to use your own dsn, and to ensure you have write permission on the directory (here the directory is called products)


<%
set objconn=server.createobject("adodb.connection")
set objrs=server.createobject("adodb.recordset")
objconn.open("your_dsn")

set fso=server.CreateObject("Scripting.FileSystemObject")

const fc_path="products\"
const fc_OverWrite=true

sql="select product_id from products"

set objrs=objconn.execute(sql)
if not objrs.eof then
while not objrs.EOF

sf_name=objrs("product_id")&".asp"
fc_hostpath=Request.ServerVariables("Appl_Physical_path")

set sf_stream=fso.createtextfile(fc_hostpath & fc_path & sf_name,fc_overwrite)

sf_stream.writeline "<" & "!-- #include file=" & chr(34) & "product.asp" & chr(34) & "-->"
sf_stream.writeline "<" & "%call show_product(" & objrs("product_id") &")%" & ">"

sf_stream.close

set sf_stream=nothing
objrs.Movenext
wend
end if
%>

So now, instead of calling /products/product.asp?product_id=351 you simple call /products/351.asp

but best of all, you still have all your code in one page (product.asp) so any global changes are still just as easy to make.

Dreamquick

8:33 am on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can actually make a much better version using a custom 404 error handler as you can avoid having a lot a physical files around - you just parse the soruce URL and make the db spit out the appropriate information to a "virtual" page.

- Tony

Scampi

9:58 am on Apr 28, 2003 (gmt 0)

10+ Year Member



Thats quite a dirty little trick, im going to remember that one : )