Saturday, May 22, 2010

Can i use ASP in a URL?

This works


%26lt;!--#include virtual="XX-AREA-XX/hotels/XX-TOWN-XX_pr...


But, i want to do this


%26lt;!--#include virtual="%26lt;!--#include virtual="XX-AREA-XX_name.asp"--%26gt;/hotels/...


I carnt get it to work, ?????


Please help.


Mark C

Can i use ASP in a URL?
You cannot use include a dynamic include inside a %26lt;!--#include





The reason that in Active Server Pages the include directives are resolved before the server-side script portions are processed. so The above code listing will trigger a nasty error telling you that the include file cannot be found.








But the below is a workaround








In order to get around this problem, the Microsoft FileSystemObject Object can be used to load and pass the content of the desired include file into a string variable which can be inserted into the page that is being sent to the client. The following function, "getFileContents", aids in this process by loading a file that is passed to the function as an input parameter and returning its contents as a string.





%26lt;%





'Pass the name of the file to the function.


Function getFileContents(strIncludeFile)


Dim objFSO


Dim objText


Dim strPage








'Instantiate the FileSystemObject Object.


Set objFSO = Server.CreateObject("Scripting.FileSyste...








'Open the file and pass it to a TextStream Object (objText). The


'"MapPath" function of the Server Object is used to get the


'physical path for the file.


Set objText = objFSO.OpenTextFile(Server.MapPath(strIn...








'Read and return the contents of the file as a string.


getFileContents = objText.ReadAll





objText.Close


Set objText = Nothing


Set objFSO = Nothing


End Function


%%26gt;





Using this function "dynamic include" files can be achieved. First, the main page (a template file containing the page layout and any static content) would be loaded and passed to a string variable. Then, the contents of the include file would be loaded and passed to a string variable. Finally, the variable containing the contents of the include file would be inserted into the contents of the variable containing the main page.


Example: Dynamic Include Files





First, lets take a look at the "template" file. In this listing, there is an HTML comment, "%26lt;!-- INCLUDE FILE HERE --%26gt;". We will replace this HTML comment with the content of the include file.





%26lt;html%26gt;


%26lt;body%26gt;


%26lt;h2%26gt;Welcome to my web page!%26lt;/h2%26gt;


%26lt;table width="500" border="1"%26gt;


%26lt;tr%26gt;


%26lt;td%26gt;


%26lt;!-- INCLUDE FILE HERE --%26gt;


%26lt;/td%26gt;


%26lt;/tr%26gt;


%26lt;/table%26gt;


%26lt;/body%26gt;


%26lt;/html%26gt;





Now, lets look at the include files that will be used for this example. The first include file is the default include file. The default include file is a form that allows the client to choose which of three include files to load. Notice that the "action" attribute of the form is omitted. This is because when the form will be submitting to itself (causing "dynamicinc3.asp" to reload).





%26lt;!-- BEGIN DEFAULT INCLUDE --%26gt;


%26lt;form method="post"%26gt;


%26lt;h3%26gt;Select the name of the file you wish to load%26lt;/h3%26gt;


%26lt;p%26gt;


%26lt;select id=cboFile name=cboFile%26gt;


%26lt;option value="includefile1.inc"%26gt;File #1%26lt;/option%26gt;


%26lt;option value="includefile2.inc"%26gt;File #2%26lt;/option%26gt;


%26lt;option value="includefile3.inc"%26gt;File #3%26lt;/option%26gt;


%26lt;/select%26gt;


%26lt;input type="submit" value="Submit"%26gt;


%26lt;/p%26gt;


%26lt;/form%26gt;


%26lt;!-- END DEFAULT INCLUDE --%26gt;





For the sake of this example, the content of the other three include files has been kept very simple.





%26lt;!-- BEGIN INCLUDE FILE #1 --%26gt;


%26lt;h2 style="color:red"%26gt;FILE #1 CONTENTS%26lt;/h2%26gt;


%26lt;br%26gt;


%26lt;a href="dynamicinc3.asp"%26gt;Return to default page%26lt;/a%26gt;


%26lt;!-- END INCLUDE FILE #1 --%26gt;





%26lt;!-- BEGIN INCLUDE FILE #2 --%26gt;


%26lt;h2 style="color:green"%26gt;FILE #2 CONTENTS%26lt;/h2%26gt;


%26lt;br%26gt;


%26lt;a href="dynamicinc3.asp"%26gt;Return to default page%26lt;/a%26gt;


%26lt;!-- END INCLUDE FILE #2 --%26gt;





%26lt;!-- BEGIN INCLUDE FILE #3 --%26gt;


%26lt;h2 style="color:blue"%26gt;FILE #3 CONTENTS%26lt;/h2%26gt;


%26lt;br%26gt;


%26lt;a href="dynamicinc3.asp"%26gt;Return to default page%26lt;/a%26gt;


%26lt;!-- END INCLUDE FILE #3 --%26gt;





Finally, consider the ASP file that makes the whole example run, "dynamicinc3.asp".





%26lt;%





'-------------------------------------...


'The "getFileContents" function should be included at the top


'of the ASP file.


'-------------------------------------...





'Declare variables to hold the content of the main page and


'the include file.


Dim strMain, strInclude





'Get the contents of the main page and pass them to the "strMain"


'variable.


strMain = getFileContents("maintemplate.inc")





'Test to see if the "cboFile" select box is being submitted. If so,


'load the requested include file. If not, load the default include.


If Request.form("cboFile") = "" Then


strInclude = getFileContents("includedefault.inc")


Else


strInclude = getFileContents(Request.form("cboFile"))


End If





'After the proper include file contents are loaded ("strInclude"),


'then insert it into the main page ("strMain") using the "Replace"


'function.


strMain = replace(strMain,"%26lt;!-- INCLUDE FILE HERE --%26gt;",strInclude)





'Use the "Response" Object to "Write" the completed page to the client.


Response.Write strMain





%%26gt;





This sample works, and, in effect, creates the ability to use dynamic include files because it does not use the include directive. Instead this example uses the FileSystemObject Object.


No comments:

Post a Comment