Cannot call Request.PhysicalApplicationPath on VB.NET - asp.net

I'm trying to request the path of my application to save new files on it, I know I should do something like this:
Dim mypath As String = Request.PhysicalApplicationPath
But for some reason I cannot even find the Request class.
I read it belongs to the System.Web namespace, I added it and still not working. Any ideas?

You can use Server.MapPath("~/logfiles") to get the path of the subdirectory logfiles in the directory where your web pages are.
Is this in an actual ASP.NET web page, or in a handler?
EDIT
Another way is to use Path.Combine(HttpRuntime.AppDomainAppPath, "logfiles")

Related

Response.WriteFile adding directory path where it shouldnt

I have a web page that is displaying a PDF file with the following code:
Response.Clear();
strFilePath = Server.HtmlDecode(Request.QueryString["filename"]);
Response.ContentType = "application/pdf";
Response.WriteFile(strFilePath);
The filename got from Server.HtmlDecode() is "\FileServer\shared\faxqueue\fax.pdf"
However an exception is thrown for directory not found and it says that it cant find the file. It also says in the exception that it is looking for: "C:[Website Root Folder]\FileServer\shared\faxqueue\fax.pdf"
This means that it has appended the filename given to the folder where the website code is located.
How can I stop it from using the website root?
Thanks
That is true because you ask it to do so.
It is a bad idea to pass in the direct file name using the query parameters.
You can of course create a direct path to the file you are using instead of this relative path:
string absolutePath = Path.Combine(#"C:\yourRootFolder", strFilePath);
Response.WriteFile(absolutePath);
But as said, I warn you for the security risks! You have to grant the IIS application pool user access to the folder you specify here. Your files can be easily hijacked by passing in something like:
..\..\..\Windows\anysecurefile.txt

Change "~/foo" to "approot/foo" from inside Global.asax

In the global.asax of my MVC app, I have a string like "~/foo". I have to generate a client-side script (JavaScript) and pass the value of this path "~/foo" to that script.
However, before passing that path, I want that the path must resolve to "approot/foo" where approot is the application's root.
So, for e.g. I deploy my application in IIS under a new website named Ding (for want of a better word), the path "~/foo" must resolve to "/Ding/foo".
How do I do this?
You can use the VirtualPathUtility class there's a bunch of methods there to help you.
In your case you want VirtualPathUtility.ToAbsolute().
For example:
var path = VirtualPathUtility.ToAbsolute("~/foo");
Should resolve how you want.

ASP .NET invalid path

I have a web page which prompts the user for an excel file using the fileupload control. What it then does is read the file into a datatable using an OleDbConnection and then runs other code with that data. When I test in Visual Studio, it works fine. For example, I can look up a file 'g:\myfiles\upldtest.xls', it finds the file, reads it and the code works. When I try to run it on our web server, I get an error when it tries to create an OleDBConnection saying It is trying to create an OleDbConnection and the path 'g:\myfiles\upldtest.xls' is invalid.
I tried to use ManagementObjectSearcher to convert the connection string path to UNC (\\MyDataServer\myfiles instead of g:\myfiles). When I test it, it shows the correct path but when I upload the page to the web server, I still get the path 'g:\myfiles\upldtest.xls' is invalid.
The code I use to determine the required file name is this
string tname = FileUpload1.PostedFile.FileName; //the file name and path
string gname = tname.Substring(tname.LastIndexOf("\\") + 1); //The path name
Any ideas what I am missing? My contract requires me to use VS2005 and .NET framework 2.0 so, I can't use anything newer. Thanks in advance for the assistance.
HttpPostedFile.FileName returns the fully qualified name of the file on the client machine.
You need to call SaveAs() to actually save the file on the server:
using System.IO;
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string filepath = Path.Combine(#"X:\Your\Own\Upload\Folder", filename);
FileUpload1.PostedFile.SaveAs(filepath);
// Now use `filepath` as your data source.
IIS might have already written the file in a temporary location to save memory, but since you can't (and shouldn't) access that location, it makes no difference.
You should also be aware of cross-browser issues. IE sends the whole path to the server on file upload, while Firefox/Chrome do not.

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#?

Where does the ASP.NET development server get its images from?

In my web application I dynamically generate images and want to show the images as part of a web page.
But, when debugging using the ASP.NET development server (not IIS) - I have no idea where to store these images so they can be referenced from my web application.
Should I use Directory.GetCurrentDirectory()?
Or Assembly.GetExecutingAssembly().Location?
Or Assembly.GetExecutingAssembly().CodeBase?
None of these paths seem to work.
Any ideas?
Server.MapPath("~/") will give you the path the the root of the web application. Everything placed within the scope of this path should be automatically exposed by the web server, be it IIS or the VS devevelopment server.
The Server property is available in to all Page objects, and can otherwise be found on the HttpContext.
You can use
Server.MapPath()
You can write ashx handler to server images on the fly. Here is tutorial for this. It you want to generate and display them later then create a folder in your web-site folder tree and get its physical path using this:
string imgPath = Server.MapPath("/") + "Images\" + IMAGENAME;
Make a seprate aspx page, and put your code overthere that create your dynamic images..
your code should be look like....
Response.ContentType = dtblProductImage[0].ImageFileExt;//image extension
Response.BinaryWrite(dtblProductImage[0].ImageData); //your image binary
now go to that page where you want to show your image and set the image property of imageURL to your page, it will look like this....
<asp:Image ID="imgProduct" runat="server" ImageUrl="yourpage.aspx" />

Resources