Why does ASP.NET publish my local folder paths? - asp.net

In a common ASP.NET Web Forms page, I specify the location of a backup file as follows:
string backupFilePath = "../data/backups";
ICronjobTask[] tasks = new ICronjobTask[]
{
new FileBackupCronjobTask(backupFilePath)
};
The ICronjobTask is executed later on. When calling the published page on my web server, an error message tells me that parts of the path "C:\Windows\SysWOW64\inetsrv\data\backups\file.txt could not be found.
Why does ASP.NET publish my local directory structure? How do I turn this off?

You're displaying the raw exception error message, is my guess. Whenever you do that, you risk giving away information that you don't want to be made public. Instead of doing that, you should interpret the exception and give general information to the user.

The solution was to use the Server.MapPath(string path) method that returns the physical path corresponding to the specified logical one.

Related

Not able to download file name containing 'aspx' character in asp .net

Am developing asp .Net web application.
In one of my aspx file am using file to download using generic handler. Everything works great. But when am testing i felt a strange problem. That, if am uploading a image or document with file name containing aspx character for Eg; aspxphoto as file name.
Uploading doesnt have any any problem but when i try to download it is throwing error in Generic handler file as
Object reference not set to an instance of an object.
Can anyone help me why this problem happends and how can i fix it?
You will not be able to do this. The IIS handler wants to "handle" the ASPX. You should simply not allow it, or if you have to, rename it to .aspx.uploaded or something. If you allowed, it you could open yourself to hacking.
As another option, you may be able to create a virtual that implements ("no processing") - possibly using the HTTP Handler under the virtual and just disabling script / execute permissions (under handler >> Edit Feature Permission >> Script OR under Virtual >> Edit permissions >> Special >> Turn Execute off.
I would not recommend the last, since it will add complexity when migrating between test and live AND for recovery (DR).

Access to the path 'c:\windows\system32\inetsrv\19.xml' is denied

I have a grid view on page. When i click on a select button of grid view. I am getting an exception like this
Error Message: Access to the path 'c:\windows\system32\inetsrv\19.xml' is denied.
here 19 is the id of the record in the grid view.
Actually it was working fine till yesterday. but suddenly raised the exception.
Every thing here is done by a web service.Web service works fine on all pages but only on one page.The exception is raises only on production system not on development system
It is occurring on only one page. How to resolve this.
Check your create/write/reading rights on the folder the xml is in.
Your application pool identity probably does not have the appropriate rights to create/modify/open the file, causing this error to occur.
Please check the security settings and see if that fixes the error.
You need to use Server.MapPath if xml documents are located under the root of web-app.
string value="19.xml";
string file=Server.MapPath("~/folder/" + value);
...
Try to move your XML data to a DataTable.. hence remove neediness to access directly your file. When everything is done simply save back your changes into the respective xml file.

iis7 / asp.net not showing modification of a file

I have currently moved my asp.net 4.0 web application onto IIS7 (windows 2008 server). I used a test folder to hold files from a directory on the localhost machine, however; when I moved the aspx file over and changed the code(on the server), the application keeps throwing this error
Could not find a part of the path 'C:\Users\***\Desktop\TestFolder\'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\****\Desktop\TestFolder\'.
I only have two functions that deal with directories and neither function points to that test directory anymore.
private int checkForFileNumbers()
{
string url = "c:/***/realfolder";
DirectoryInfo directory = new DirectoryInfo(url);
return directory.GetFiles().Length;
}
private void checkForFiles()
{
string url = "c:/***/realfolder";
DirectoryInfo dir = new DirectoryInfo(url);
foreach (FileInfo files in dir.GetFiles())
{
FileDropDownList.Items.Add(files.Name);
}
}
I have tried commenting everything in this aspx file out, only to find that this problem still occurs. I performed the issrestart from the cmd line and nothing changed. Any help would be appreciated.
This is a deployment problem. Your error message is clearly out of sync with your current code. The actual code that is being run from IIS must be coming from an out-of-date binary. As a starting point, I would delete all DLLs in your bin directory on the server and redeploy fresh DLLs. The may also require you to refresh your aspx files depending on what kind of project type you're using.
Once you get this sorted out, I would take another look at how you are deploying updates to your site. Visual Studio and IIS 7 offer some neat deployment options if you want to go that way. Also, you could deploy via Git or SVN. But deployment by manual replacing single files is going to lead you to the type of trouble you are currently experiencing.
If realFolder directory exists under root of your web-app or itself is root then use Server.MapPath method to get real path of virtual folder.
For instance,
String rootPath=Server.MapPath("~"); // It will return real path of current web-app's root.
//OR - In case if want use Server property from separate code file.
String rootPath=HttpContext.Current.Server.MapPath("~/testFolder");
Since you're not referencing the path in your code, it's probably either in one of your config files, HttpModules, Global.asax, or perhaps a DLL.
What other components are you using?
Before moving it to a server, have you tried running the site under IIS on your local machine? Maybe attach a debugger and see where the error is thrown?
Oh, FWIW, the IIS AppPool identity will need to have read permission for your C:/***/whatever folder.

Getting web address for local file in ASP.Net

Is there a 'correct' way to get the proper web address for a file under an ASP.Net application? For example, I have content in '/Content/Images/Gallery/2010-01-17/small/', and I would like to iterate through all of those files, and output to the browser a link.
Now, I can do it manually by working out the path from the files FullName or I can do it from knowing the current directory, but is there a proper ASP.Net way to do it?
As you can probably tell, I'd rather use the provided method if it exists :)
Regards
Moo
You can use the method ResolveUrl() for that. If your content directory is located directly under you web app's root directory, then this should work:
// "~" results in an URL to your web app's root directory
string imageBaseUrl = this.ResolveUrl("~/content/gallery/2010-01-17/small");
Then you can append the names of the images to that base URL.
I think ResolveUrl is only part of the answer.
Unfortunately, there is not a built-in function to return a full URL to a particular resource, inclusive of hostname and protocol. Part of the reason for this is that you can access a URL any number of ways... and the server is completely agnostic of the hostname. You have to look at either the Request.Url properties to build a new URL from the user's request, or use ServerVariables.
See this question:
How to Convert "~/default.aspx" to "http://www.website.com/default.aspx" C#?

Culture name is not supported

I'm receiving "culture name 'uploads' is not supported" when my ASP.NET application start. Where do I have to view/debug to toggle the error?
A full-text search for "uploads" returns 0 entries in my project.
I've deleted this folder and it solved the problem:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\8dfb04ef\44bf70fb\uploads
Do not worry, it is not a real error.
When the runtime tries to find which resource assemblies exists, it tries to enumerate folders where compiled DLL files reside. It assumes the name of the folder is the culture name, so it tries to create a culture for it. Unfortunately uploads is a folder which is created automatically by ASP.NET, and it is obviously not a culture :).
The runtime ignores some predefined folders like "hash", but not "uploads". So we have to live with it.
I am getting the exact same error. ["culture name 'uploads' is not supported"]. I do not know what is causing it but I've been able to get around it by clicking [Debug|Exceptions (Ctrl-alt-E) and un-checking "Common Language Runtime Exceptions". I do not have an 'uploads' folder or file in my project and there is nothing in my project that has anything to do with multilingual support and the error fires right off the bat when I run my project ... looking at the call stack just walks me through a whole lot of mscorlib calls. I have no clue why it's even getting called.
JumpingLethalRampage, instead of unchecking all "Commong Language Runtime Exceptions," you could uncheck just "Common Language Runtime Exceptions > System > System.ArgumentException." You don't need to uncheck all CLR items.
It sounds like maybe you have some code somewhere that is trying to set the culture based on a URL pattern (this may be tracked down if it is possible to get a stack trace). For example, sometimes I'll set up multi-lingual sites with URLs like this:
http://www.example.com/en-US/
http://www.example.com/es-MX/
I'll then have code in my base page that looks at the URL and attempts to set the culture based on the URL path. But if you tried to hit a URL like:
http://www.example.com/uploads
It would fail with an error like what you are seeing if you just blindly take the first path element and try to use it as the culture without checking it first.
If the application is it built upon DotNetNuke (and not a simple ASP.NET application), this kind of error generally occurs in DotNetNuke due to the issue described in Culture name '__page' is not supported.
If you are not using DotNetNuke, check your Application_Start code (in the Global.asax file). It might be setting the CultureInfo. Or check your web.config file for any invalid culture values.
Assuming that your search through the project wasn't restrictive (widest possible search in all files), then my guess would be that your browser has a User language set to a custom string - "uploads".
In IE, you can check via Tools -> Options -> Languages -> Add -> User defined language.

Resources