Reputation: 1016
I would like to receive files from an asp.net website but I would like to verify the request with data in a database before serving the file to the client.
For images I would be loading using a standard html image tag like this:
'<img src="/Uploads/RANDOMGUID/img.aspx" alt="The Moon" title="The Moon">
The requests will have a random GUID in the path which maps to a field in the database for the stored image.
I would like to have one entry point for all the requests this could be an aspx or ashx page for example [unless there there is a better way], this would then:
How should I implement such functionality? Do I need a site wide file extension which is handled in an aspx file and returns a custom file content type?
Upvotes: 0
Views: 704
Reputation: 966
First, put image files into directory that can't be accessed by user directly.
Then change path like this:
<img src="/Uploads/img.aspx?id=RANDOMGUID" alt="The Moon" title="The Moon">
That way, you can get ID from Request.QueryString["id"] and do the checking.
It's much easier than path you wanted because it will always go to same aspx page. With original path you would have to do path mapping and complicate your life without need.
After checking in database you can use Response.WriteFile to send appropriate file. Check https://msdn.microsoft.com/en-us/library/dyfzssz9%28v=vs.110%29.aspx for details.
EDIT: This was regarding getting ID of image. As @mason pointed out, you should register handler and use .ashx extension as you don't really need view.
Upvotes: 1