Server.MapPath isn't pointing to the actual server path when deployed - asp.net

I put the server.MapPath code in ASP.NET VB code behind.
On my laptop i compile and build. However when i upload my web application to the actual server. I received error where the link is still referencing the laptop link.
How can i solve this issue?
My code is
Server.MapPath("./scorecardsheet/db.xls")
Actual server link is c:\Inetpub\vhosts\xxx.com\httpdocs\xxx\scorecardsheet\db.xls
but when i run the page on the server it shows the link of my development platform as c:\user\jerry\visual studio 2010\projects\xxx\xxx\scorecardsheet\db.xls

You probably want Server.MapPath("~/scorecardsheet/db.xls") instead, yes? Note the special ASP.NET tilde (~) symbol instead of the current directory symbol (.) This way it will calculate the relative path based on the location of the web site root wherever it is on the system.

Related

ASP domain and files

I have no idea about asp, but I had to do some modifications in a web site, an easy modification. So I downloaded all files from server and I did all the modifications in Visual Studio 2013. Then I tested each page in the local host and it was perfect.
When I uploaded the files, I created a folder called "development", to tested it before I changed in the real site, so, my real site is for example "www.realsite.com" and my new folder is inside, with all the file, so I write in my url "www.realsite.com/development" and it shows the page, but not the one I had modified, but the real site. I want to know if there is a config file to change the path of the development site to see the changes I make and not the real site, because if I click in the development site a menu, it sends me to the page in the real site.
I hope you can help me with this,
Thank you!
PS: Do you know what is the meaning of "~/" in for example : src="~/folder/folder/xxxx.xx"
This is because the URLs in the project are using absolute paths, all pointing to the root. If they were using relative paths, moving the project to a folder and running it from there would work just file.
The difference:
... <-- absolute, note the leading slash
... <-- relative, no leading slash
Well, it depends on what kind of changes you are referring to, what kind of ASP.net site (or application).
The ~/ in ASP.Net means "path from application root". A subfolder (the new folder you created) in an existing application is just that, a folder. It is not "another application root". So if the existing code refers to "its root", e.g. where it uses ~/, it's probably not what you would expect..
Again, not enough info, but if you experience more unexpected behavior, it will probably be because of this (application scope).
Ref: ASP.NET Web Project Paths
ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application.
Hth...

using Classic ASP INCLUDE VIRTUAL in ASP.NET page

I maintain an Classic ASP intranet site. I've developed a new page in ASP.NET that has a link to it from the old site. I would like to use the INCLUDE from the intranet which puts a header with menus on each page. I get a compile error when I run the new page in the debugger. The INCLUDE file contains nested INCLUDE files. The error says it can't find the nested includes. It's looking for them in the C:\xxxxxx when the actual physical path is on the d:\ drive.
Apparently it's resolving the INCLUDE VIRTUAL for the top level include, because it's looking for the nested includes.
Why does it resolve the first include, finding it on the D:\ drive, but is looking for the nested includes on the C:\ drive?
here's the code for the top level include
< !--#include virtual="/includes/page2header.asp"-->
here's the code for the nested includes
< !-- #INCLUDE virtual="/inc/menustyles.txt" -->
< !-- #INCLUDE virtual="/inc/Config.asp" -->
The site is running on IIS 7.5.
The site is located on the server on the default website in a virtual directory in the path
D:\inetpub\wwwroot
The compiler is looking for the nested includes in this path with this error,
Could not find a part of the path 'C:\inetpub\wwwroot\inc\menustyles.txt'
Include files don't work in the same way in ASP.NET as they do in classic ASP. When you use the Include directive, it results in the file content being rendered as plain text in the ASP.NET page. You will have to take an ASP.NET route to solve your problem. Typically, User Controls are used to render snippets of reusable HTML.
See my article on this topic for more information: http://www.mikesdotnetting.com/Article/144/Classic-ASP-Include-Files-in-ASP.NET
The problem I was having here is I was running the debugger on my development machine, and it's virtual directory IS on the C:\ drive for this site, and the first level virtual file did exist in the path where the compiler was looking for it, and the nested files did not exist. The problem was solved when I copied the nested files into place on the development machine.
However, that raised a new problem. The nested files contain server side script, Classic ASP VBScript, and it just won't run in my ASP.NET page. This problem brings this effort to a dead end, unless someone can recommend how to solve this new problem.
Thanks, Bren

File not found error in Asp.net 4.0

On Local host,my solution works fine but on server when i deploy ,it gives that file not found error. and the error message is something like this.http://localhost:27375/favicon.ico
I don't have any such file in my application.I tried to create on in the root folder but no luck.any help is highly appreciated
Your favicon.ico file should be in your root directory for the web site.
It looks like your application is still trying to connect to the localhost in order to display the favicon. Are you hard-coding your URL to the favicon with a http://localhost:27275/favico.ico address?
To build on what Internet Engineer said, using ASP.NET, you can use a relative path prefaced with ~ or /. So, you can use ~/favico.ico or /favico.ico in order to reference your icon file.
First check in web server if you can see the file directly in the browser:
http://productionwebsite/favico.ico
If the file is there, now you need to check that the code is using relative paths. Most likely this is coded using absolute paths.

Find the path of browser default downloads folder

how can we find the path of browser default downloads folder in c# / asp.net?
For example I can get the path of user desktop like :
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Thanks..
You can't find that out in a web application. It's up to the user to decide which browser to use and how to configure it and where to save downloaded files by default and you have absolutely no way of interfering or even knowing his choices from a web application.
First of all looking at MSDN on Environment.SpecialFolder there is no download folder, and the reason is that this is different for every browser.
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
And there have nothing to do with asp.net, if you look it from the server side you just get a directory on nowhere, meaning that this have nothing to do with the web application that run under the pool.
What you can do
You can use the HttpRuntime.AppDomainAppPath and use it to know where your site lives, and there place a "download" directory and use this full path:
HttpRuntime.AppDomainAppPath + "download/"
for download/upload files.

Classic ASP Server.MapPath() doesn't work as expected in global.asa

In Classic ASP, Server.MapPath() doesn't always work properly in the Application_OnStart event within global.asa. I have an ASP page at "\testfolder\test.asp" within a virtual root, I have an XSLT file at "\xsl\transform.xsl". My virtual root is located in "c:\inetpub\wwwroot\testapp\".
I use MapPath within the ASP page to get the full path to the XSLT file. The call is:
sXslPath = Server.MapPath("xsl\transform.xsl")
Some times MapPath returns "c:\inetpub\wwwroot\testapp\xsl\transform.xsl" as expected, other times it incorrectly returns "c:\inetpub\wwwroot\testapp\testfolder\xsl\transform.xsl". The incorrect path obviously causes serious problems.
I am answering my own question here:
This problem occurs because when called in Application_OnStart, MapPath incorrectly includes the context of the page that caused the application to startup. If the first ASP page to be run when the application isn't yet started is not in the root of the virtual root then MapPath gets confused and adds the path to the called ASP page to the path it returns.
So for example if the page that started the app was in "c:\inetpub\wwwroot\testapp\folder1\folder2\test.asp" then MapPath would incorrectly add "\folder1\folder2" into the middle of the path and return "c:\inetpub\wwwroot\testapp\folder1\folder2\xsl\transform.xsl"
If your website only has files in the root folder or doesn't use MapPath in global.asa then you will never notice this little oddity. I suspect there are lots of ASP Classic sites out there that fail to startup properly sometimes because of this, but their owners just do a quick iisreset, not knowing what quite went wrong.
The result of this is that you can't reliably use MapPath in global.asa if you have a website that has ASP files anywhere other than just the root folder.
If it is a one-off website then the easiest solution is to just hard code any paths you use in global.asa.
If you sell a product to other people based on ASP Classic then hard coding the paths is not an option. You either have to move all usage of MapPath out of the application startup or deal with the issue by writing paths into your ASP files as part of the installer.
alternatively use
sXslPath = Server.MapPath("\xsl\transform.xsl")
which will then map the path from the root directory
naturally if you are developing on iis in a OS that is not a server, root will be the default website, you would have to remember to change on deployment...

Resources