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

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.

Related

ASP.Net Accessing Server Filesystem

I am having trouble accessing information on the server my website it on. As the website was originally programmed with VB.Net, I cannot change the language without having to completely reboot the website. The way the website and server are configured, I can only use ASP.Net and VB.Net.
I am needing to add a section where they can create folders, edit folder names, and upload pictures and text documents on the server through the public website. I tried using parts of the FileIO, Server, and Http that should have worked, but none of them did. Most of my research is about local files and text documents.
I have not been able to find any information that works. Can someone help me? Thank you.
Firstly, creating a virtual directory in ISS mapped to somewhere on your disk would be a good start. This way you have a separate folder for user data in a folder with write access (make sure IIS has write access to the folder!), and the folder is not affected by website deployments.
Secondly, you might need to resolve absolute path for most of the System.IO.File calls. See How to convert a relative path to an absolute path in a Windows application?, just you will need to convert this code to VB.

How does people make ASP.NET page in URL with html file name?

I seen an ASP.NET application, in the URL is saying:
http://xxxxxxxxx/FILENAME.html?xxxx=xxx
How come it is html file? But not aspx file? How did they do it?
I heard from my manager that's an ASP.NET project he outsourced.
Sometime I seen people with their web page is ended in .html too, but obviously that is generated dynamically...
Files ending with .html are optional. These are static HTML-pages without any code-behind and can be included as part of any web application. They are not parsed and compiled by the server but rather just sent as good old predefined HTML.
You can also configure the web server so that it routes requests with different endings through the ASP.net rendering engine. This way you can keep the widely recognized ending .html and still have dynamic page generation.
The file extension is not necessarily tied to the execution engine. You can make ASP.NET process .aspx, .html, .htm, .bob, .foobar, .css, etc.
There are multiple of ways to do this:
In IIS manager, set the file extension mapping for .html to point to ASP.NET. If you're using MVC, you can handle this via routing.
Use a rewrite engine to map anything with a .htm* extension to .aspx
There are probably other ways, but these are the most direct.
Also, the .html extension doesn't mean that the file was dynamically generated.
You can use URL rewriting. There are a lot of different rewriters most popular being the URL rewrite module ( http://www.iis.net/download/urlrewrite ) and the built in (in ASP.NET 4.0) Routing Engine ( http://msdn.microsoft.com/en-us/library/cc668201.aspx ).
The URL Rewrite module is external to your application and it translates incoming URLs to regular .aspx URLs. You are responsible for generating the links with .html. It is good if you are adding it to an existing application.
The built in routing can generate urls based on routes and is configured in Global.asax (usually) with code.
Right click on the project.
Add new...
pick the HTML file type.
Some people prefer to use a different extension (or even none at all) in order to hide the technology used to develop the site.
Bear in mind that you would have to properly configure IIS to let the .net engine handle the .html file types.

Access to dynamic images on server

I'm developing a web application in which users uploads images and I'm keeping the location of the uploaded image path on server. For example:
C:\fix_directory\a8531.jpg
In my page, I want to display this image but I couldn't. I've tried many things but couldn't find a way of doing it.. This directory isn't part of my project because it will be always updated.
What is the effective way of overcoming this problem?
Thank you
You could setup a virtual directory (yourweb.com/images) which points to your images, or even a web (cdn.yourweb.com).
You could also write a HTTP handler or a file handler (.ashx) in ASP.NET to serve your images from that location.
Update
I guess the most effective way of overcoming this problem would be to store the uploaded files simply somewhere in your web directory.
Update 2
I'm definitley sure, the most effective way of overcoming this problem is to have a real IIS or a Visual Studio Development Server and not trying to do anything with the Vista Home Basic pseudo IIS which can do nothing.
The simplest way would be to make fix_directory a virtual directory within your project inside of IIS, and access the images that way.
A more complex approach would be to do something in the lines of pulling in your image via a FileStream, and outputting it to your website via a ResponseStream.
A common way this is achieved is by creating a generic handler (.ashx) that's only responsibility is to output your images.

How do I download an msi file via an asp.net button?

So, I've created my wonderful winforms app that I want to unleash upon the world, and now I am trying to create a simple website to host some basic information and link to the setup file (msi installer file )....
I have a button on the asp.net page and the setup file setupApp.msi in same folder as the asp.net page. I am currently trying the following:
Response.Redirect("http://./SetupApp.msi");
But this best guess at what to do is not working. Is there something wrong with Mime types here? What do I need to put in the click event to allow users to download this file?
The path you are passing in to the method is not valid (there's no server name called ".").
You can pass in a relative path and it should work fine because ASP.NET will resolve the path:
Response.Redirect("SetupApp.msi")
Or if it's not in the same folder, try one of these:
Response.Redirect("../Downloads/SetupApp.msi")
Response.Redirect("~/SomeFolder/SetupApp.msi")
Keep in mind that you don't necessarily have to do the whole redirect at all. Instead of writing code in an ASPX file you could just have a link to your MSI:
Download my app!

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

Resources