HttpHandler and folder locking problemin IIS7 - c#

I have a simple HttpHandler which gets the image file from the specified path in the callig URL. For instance, when this URL: http://www.abc.com/images/imageview.ashx?fileName=ok.jpg is called, it will write the file from the path: d:\images\ok.jpg using response.writefile.
Everything works fine, but the folder named images in the web site root path (d:\inetpub\wwwroot\images) is locked and after the first call, I could not rename or delete the folder. I don't use the folder (in the web site root) any where!
I've checked all files for the second folder and they are not in used, only the folder is in used.
When I changed the calling URL to http://www.abc.com/imageview.ashx?fileName=images/ok.jpg the problem resolved.
It seems that IIS7 locks the virtual folder for ashx URL.
Any idea or experience?

It's because the image is resolving most likely relative to the folder the handler is in. Either put an absolute path, like /images/ok.jpg, or on the server call ~/images/ok.jpg to resolve it properly. See more about ~ here, http://weblogs.asp.net/fmarguerie/archive/2004/05/05/avoiding-problems-with-relative-and-absolute-urls-in-asp-net.aspx

Related

How to restrict json files that are stored under wwwroot folder to view from Web Browser

I have a "client-styles.css" in my wwwroot / client_folder folder. When I run my project in localhost and give that css file path in the browser its showing the entire css file in the web browser.
( localhost:5000/client_folder/client-styles.css ).
In the same way I have a folder also contains json files which contains sensitive information and those are also showing if we give the path to those Json files ( localhost:5000/client_folder/client-secrets.json ) in the browser. Is there any way restricting some files to view from web browser.
Thanks
Based on your last comment above:
It's perfectly acceptable to store css and javascript files in wwwroot. However, do not store anything secret there. Storing secrets like connectionstrings are best in EnvironmentVariables.

nancyfx unreachable exist file

I use nancyfx.
I have published a folder with files.
I start the server and observe the following situation:
If there is a file '1.html' in the published folder, then I get it through the browser. If I delete this file from the disk, I get an error 404 (this is correct). If I add this file to disk again, or I change its contents, I normally get it in the browser.
If after starting the server I try to access from the browser to a nonexistent file '2.html', I get an error 404 (This is correct). However, if after that I create the file '2.html' on the disk, I still get a 404 error. It helps only restart the server.
I got the impression that nancyfx at the first access to the requested files forms some kind of cache, which subsequently does not allow me to get the files added after they were unsuccessfully requested.
Help please with the decision of the given problem. Thank you in advance.
Nancy uses a convention based approach for figuring out what static content it is able to serve at runtime. you can check the Documentation here

Upload file to a folder on other asp.net mvc website

I have two asp.net MVC websites. One the front end(mysite.com) and one the backend(admin.mysite.com).
They both use the same database and everything is working fine. But, I am facing the issue in upload. I want to upload images in front end content folder from the admin website. How can that be achieved?
Using Server.MapPath("~/Content/Images/Products/product") locates the folder in admin website.
If you have access to the iis server, you could set up a virtual directory that is pointed at the desired folder on your front end site.
All that would be required from the front end is that you point the upload path to the virtual directory.
Server.MapPath("~/Files") returns an absolute path based on a folder relative to your application. The leading ~/ tells ASP.Net to look at the root of your application.
To use a folder outside of the application you can use the full path:
#"E:\Project\Folders\Content\Images\Products\product"
You can also get the full path directory:
string currentDirectory = Directory.GetCurrentDirectory();

How to give URL for a file stored in app_data folder in asp.net

I have a zip file stored in app_data folder .I want to give the path ofthat file as URL for a hyperlink.How to assign it
Server.Mappath(~/App_Data/Test.zip) will give the physical location as D:/Projects/Mnv/App_Data/Test.zip , but i want to give it as URL like http://..../test.zip, so user can download
You should not use App_Data available to the users.
This is a special folder used to store internal data only (XML, sdf, etc..).
By default, this folder is not even available via Http.
Yo can create an Action in your Asp.Net MVC and use it to retun some file from this folder. A direct download, however, it is not recommended.

Translate known physical path into virtual path

I want to take files from a known location on disk and have ASP redirect to them from code-behind, allowing the browser/app/device to control how the content is displayed.
I tried using:
Server.Redirect(pathToFile);
But got the following exception: Invalid path for child request 'C:\ContentFolder\testImage.png'. A virtual path is expected.
How can I allow my site to redirect users to these files? I am storing the base directory in the web config, and the file names are stored in a database.
If the files reside outside of your website directory you can't redirect to them. Think about the security implications if that was possible. You have a few options
Move the files inside the website directory (or a sub-directory of it). Then you can redirect your users to them using a virtual path e.g. Server.Redirect("~/files/somefile.zip").
Set up a virtual directory in IIS that maps to the physical location of the files on disk. Then you can redirect to them using the virtual path. You can do this through the GUI or config file.
Create a HttpHandler that loads the file from disk and returns them in the response. You can use a querystring to identify the file to load e.g. /filehandler.ashx?filename=somefile.zip. A quick google revealed this example.

Categories