6

I have inherited a classic ASP application. There are various things that need tidying-up but I am forced to do things gradually (I can't do a wholesale change of every script).

There are places in the system with hard-coded urls. Some scripts have to be changed before promoting to live so that the test web root name is changed to the live web root name. I am looking at ways to avoid this (essentially working out the server programmatically).

It isn't difficult. A simple check on Request("SERVER_NAME") and this sort of thing:

appName = Request.ServerVariables("SCRIPT_NAME")
i = InStr(2, appName, "/")   'keep initial "/"
 If i > 0 Then
    appName = Left(appName, i)
End If

This in an "everywhere-included" script will do the trick. Then just set up a global variable to hold the full "http(s)://server/app/" or a function to MakeUrlAbsolute() and off you go.

However, this smells a bit suspect to me. Wouldn't this be better stored at application level? This suggests setting it up in Global.asa, something like:

Sub Application_OnStart()
    Application("WebRoot") = ...
End Sub

But now I can't get hold of the SERVER_NAME so I'm reduced to having 2 different Global.asa files (one per environment).

Is this my only choice? Work it out on every request or hard-code in Application_OnStart? Perhaps using Session_OnStart would be a compromise. Or is there some clever trick to access information in Application_OnStart? Perhaps people go with the hard-coding because Global.asa doesn't change often.

Fruitbat
  • 764
  • 2
  • 5
  • 19
  • I'd say separate global.asa files for your dev and production servers is a pretty standard way to go. For example I always store my connectikon strings as app variables and use Application("dbcconn") when I need to connect to the database – John Nov 26 '13 at 15:07

1 Answers1

1

my method for ADO Connection looks like this.

'Servername for Application Root Drive
Select Case Left(Request.ServerVariables("PATH_TRANSLATED"), 1)
    Case "c" strSrvr = "80.212.207.211"   'my remote server ip
    Case Else strSrvr = "(local)"         'local Sql instance (my project on D: drive)
End Select

strConn = "Driver={SQL Server};Server="& strSrvr &";uid=DBUSER;pwd=MYPASS;database=MYDATABASE"

Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open(strConn)
Sedat Kumcu
  • 2,191
  • 1
  • 17
  • 13