Ian Williams
Ian Williams

Reputation: 55

Modify server.mappath to something else as shared hosting does not allow parent paths

I am using the following code to connect to my database. It worked perfectly until my host upgraded IIS and it now wont connect due to parent paths being disabled by default.

I have tried using the virtual path to the file instead and it just errors out every time, regardless of what I try - even after getting the full virtual path of the file directly from my hosting company.

I need to change the server.mappath part of my connection script to something which allows me to use the full filepath or url of the file, but done know where to start.

Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & 
Server.MapPath("../stockdetails.mdb")    

On most other pages its coded as follows:

filePath = Server.MapPath("../stockdetails.mdb")
objDataConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +filePath)    

If I leave everything as it is, I get the following error:

The '..' characters are not allowed in the Path parameter for the MapPath method.    

I created another file to output the full filepath of the database. This output the below:

\\e379583ad6.storage-1a.hosting.MYDOMAINNAME\sites\1a\e\e379583ad6\public_html\nurbek\stockdetails.mdb    

With this information I changed the connection script to this:

Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & 
Server.MapPath("\e379583ad6.storage-1a.hosting.MYDOMAINNAME\sites\1a\e\e379583ad6\public_html\nurbek\stockdetails.mdb")  

This then gave me the error message:

[Microsoft][ODBC Microsoft Access Driver] '(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.    

Can anyone suggest how I can modify these 2 connection scripts to use the full filepath instead?

www.mydomainname.com/nurbek/stockdetails.mdb    

Upvotes: 0

Views: 270

Answers (2)

Adam
Adam

Reputation: 926

Server.MapPath is used to convert parent paths (or a virtual path) into an absolute path. If you try to pass an already absolute path to Server.MapPath you'll receive an "Invalid Path" error, as you've already established what the absolute path is.

Using Server.MapPath is preferable as it allows you to migrate your code without having to edit any absolute path addresses, but if you're unable to use it you can just reference the full absolute path instead. This should work:

Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &_
"\\e379583ad6.storage-1a.hosting.MYDOMAINNAME\sites\1a\e\e379583ad6\public_html\nurbek\stockdetails.mdb;"

Upvotes: 0

user692942
user692942

Reputation: 16680

By default Parent Paths are disabled in IIS so most Shared Hosting Providers do not modify this. That being said you can modify it yourself using an IIS configuration file which is supported in IIS 7.0 and above.

The steps are fairly simple to implement;

  1. Create a new file called web.config in the root of the website.
  2. Add the configuration XML to set Parent Paths (the example below enables Parent Paths, Response buffering and Session state);

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <asp enableParentPaths="true" bufferingOn="true">
          <session allowSessionState="true" />
        </asp>
      </system.webServer>
    </configuration>
    
  3. Save the file (should be encoded as UTF-8).

Now you should be able to run the website without requiring any modifications to the Server.MapPath() code.


Useful Links

Upvotes: 1

Related Questions