I have got images on the web server like {sku_number}.jpg. Now I would like to have for every visitor a filename for this image like {sku_number}foo.jpg without renaming the files on the web server.
Is this possible and how?
I have got images on the web server like {sku_number}.jpg. Now I would like to have for every visitor a filename for this image like {sku_number}foo.jpg without renaming the files on the web server.
Is this possible and how?
4
Answers
If i understood you properly then one of the options would be HTTP Handler
Then register it in web.config in system.webServer -> handlers
Url.Content helper method takes a virtual content path to your file and returns a url to access the file in your site. You should not be passing a physical directory location to that.
You may consider saving the images in your web app. Create a directory called “Images” and then you can safely use Url.Content method.
For example,
Or If you absolutely need to store the images somewhere outside the app root,(Ex : “C:tempimages or a network location), You might consider creating an action method which takes the file name, read the file from the location and return as an image.
Now you simply need to call this end point and use that as the image src property.
The above is a simple solution where i hard coded it to return png files. you can update it to be more flexible (add a param to accept the file extension as well)
Note : I personally prefer to keep minimal C# in razor files. I like to move the C# code (all those Directory.GetFiles lines) to the GET action and pass the list of image names to the view via a view model/view bag(as needed).
You can create a simple controller to handle such requests.
Just meant as a simple self explanatory example, the above can be refactored to allow more flexibility. For example instead of loading file from disk the sku could be used to get the file from another data store like a database or web service.
Example of calling from View
would generate
I would recommend returning a 302 from the spoofed image to the original image, it’ll allow your web server to still properly cache the asset without you doing really inefficient reads/writes off of the file system every time the asset is requested at the .NET level, while still masking the paths in the DOM with the spoofed paths (though the original paths will be visible if that’s a problem).
Verify that the 302 is going to work for your specific needs though.
Otherwise you’ll want to basically emulate the IIS pipeline for asset caching and that can be rough.