How to refer virtual path in asp.net? - asp.net

I have created virtual directory in IIS under default web site. For example "VirtualPathTest". It refer the physical path "d:\samplelocation\VTest"
How to refer the "VirutalPathTest" in asp.net page to store file and retrieve files in that location?
The above path is not inside asp.net application.
Any one assist me to handle.
Thanks in advance.

You can achieve this by VirtualPathUtility
Ex: VirtualPathUtility.GetDirectory(Request.Path)
if you are looking to get the domain name associated with your current request, along with the virtual path of the current application, then try like this :
Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath

Related

File attachment using virtual path in asp.net

I'm working on an asp.net project which has a form with a file attachment capability. Since I'm uploading the form on a server, I can't use a physical path. How do I use the asp:FileUpload with a virtual path?
You can convert a virtual path to a physical path:
HttpContext.Current.Request.MapPath("~/Example.txt")
I'm not sure you have this quite right. The file upload control will POST the file data to your server, and it can be accessed using the PostedFile property of the control.
You then save this file (I assume) to a physical path on your server (which you can access).

Upload files to different virtual directory

I have two web application hosted on different server.For eg. one is main application and the other one is branch application.
In branch application, user will upload their files but we want to keep all uploaded files in main application virtual directory.
So that we put the main application virtual directory path while we upload the files.But We got error message like "Invalid Directory" and can't upload.
Is there any way to upload files from one application to another directly? We are using normal html upload control in asp.net and visual studio 2008.
Code Sample :
main application virtual directory "http://10.10.10.1/mainapp/uploadedfiles/"
branch application virtual directory "http://10.10.10.2/branchapp/"
HttpPostedFile postedFile;
string saveFile = Path.Combine("http://10.10.10.1/mainapp/uploadedfiles/", "File1.pdf");
postedFile.SaveAs(saveFile);
Please guide me the right way and I really appreciated it.
Thanks.
Best Regards,
Chong
In the FileUpload control, the FileUpload.SaveAs(path) method takes a physical path, not a virtual path. So if you have a virtual path, you will need to use the Server.MapPath method to convert it to a physical path. Using your example, you might want to change the last line of your code to:
postedFile.SaveAs(Server.MapPath(saveFile))
Also, you will need to make sure that the account that ASP.Net is running under has ACL write permissions on the physical directory.
I think u can share the directory and ensure both the Apps can access it, because the two apps are in different servers, the directory path maybe just like : \serverA\Files\, and the url of this directory is : http://localhost/files. u can save files to other servers by a relative path if u have the permission.

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

How do I get the complete virtual path of an ASP.NET application

How do I know the the complete virtual path that my application is currently hosted? For example:
http://www.mysite.com/myApp
or
http://www.mysite.com/myApp/mySubApp
I know the application path of HttpRequest but it only returns the folder name that my application is currently hosted, but how do I get the initial part?
The domain name part of the path is not really a property of the application itself, but depends on the requesting URL. You might be able to reach a single Web site from many different host names. To get the domain name associated with the current request, along with the virtual path of the current application, you could do:
Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
Technically, an "application" is a virtual directory defined in IIS and Request.ApplicationPath returns exactly that. If you want to get the folder in which the current request is handled, you can do this:
VirtualPathUtility.GetDirectory(Request.Path)
ASP.NET has no idea how to distinguish your sub-application from a bigger application if it's not defined as a virtual directory in IIS. Without registering in IIS, it just sees the whole thing as a single app.
Request.Url
it contains several points that you might consider to use, see the image below:
The below code will solve the purpose, however you have to do a bit tuning for two types of scenarios:
Hosted as separate web application.
Hosted as Virtual application within a web application.
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath;
In .NET 4.5
VirtualPathUtility.ToAppRelative(path)
Try this (Haven't tried it)
public string GetVirtualPath(string physicalPath)
{
string rootpath = Server.MapPath("~/");
physicalPath = physicalPath.Replace(rootpath, "");
physicalPath = physicalPath.Replace("\\", "/");
return "~/" + physicalPath;
}
Link 1
Link 2
Url.Content("~") worked great for me and is nice and simple. I used it in the view like this:
<a href="#(Url.Content("~" + attachment))">
Here my attachment is a path like "/Content/Documents/Blah.PDF".
When my app is published to a IIS site that uses a virtual directory, Url.Content("~") resolves to just the virtual directory name like, "/app-test", for example.

Programmatically deciding what file a URL should point to with ASP.NET 3.5 and IIS 7

Is it possible to programmatically resolve a URL to a file using ASP.NET and IIS? Specifically I'd like the file to be outside of my Virtual Directory (could be anywhere on the local file system). So if a URL comes in like http://mysite/somepicture.jpg I'd like to be able to return c:\mypicture.jpg. I looked into creating an IHttpModule for URL rewriting but that isn't quite what I need - it's limited to URLs within the existing site.
You cannot achieve it by URL rewriting as the file is not hosted on your Web site. You should use Response.WriteFile method in an HttpModule or HttpHandler to manually stream the file to the user.
I would like to add to Mehrdad's response by saying that you need to make sure your app has rights to the folder the files you want live in. That way you can dish it out as Mehrdad suggested.

Resources