3

I'm trying to figure out how to enable short URL's for images on the website using Application_BeginRequest.

Currently, to open an image I have to use full URL like this:

http://mywebsite.com/ViewImage.aspx?album=123&id=456

I want the images to be accessible using short URLs:

http://mywebsite.com/123/456

ViewImage.aspx retrieves images dynamically from the database.

Assuming I should be using RewritePath method. But how exactly?

j0k
  • 22,600
  • 28
  • 79
  • 90
SharpAffair
  • 5,558
  • 13
  • 78
  • 158

2 Answers2

1

You can accomplish this with URL rewrite http://www.iis.net/downloads/microsoft/url-rewrite You create rules in the web.config to map urls to files.

We use this image rule for example, to map version url's to real images. You can create something simulair for your urls. In IIS you can test you rules when you install the module

    <rule name="rewriteImgRule" stopProcessing="true">
      <match url="^v[0-9\.]+/img/(.*)$" />
      <action type="Rewrite" url="/img/{R:1}" />
    </rule>
Ivo
  • 3,406
  • 4
  • 33
  • 56
  • 1
    I think your rule is a little wonky - there's no `/img` in the path and the rewritten url needs 2 parts split. Something like `ViewImage.aspx?album={R:1}&id={R:2}`? – Basic Oct 18 '12 at 14:24
  • the rule is an example of how url rewrite works, not the solution rewrite for the provided url (because I cannot test it). Sorry should have mentioned that – Ivo Oct 18 '12 at 14:30
  • It's not available out of the box, right? Can I use it on a shared hosting? – SharpAffair Oct 18 '12 at 14:45
1

@Ivo's answer may well be the simplest but I personally prefer to avoid installing additional modules.

If you're using IIS7 (Windows 7/Server 2008 or later) you can use request handlers (.ashx files) then map various Urls to them...

See http://www.dotnetperls.com/ashx for a tutorial on setting one up.

Once you've got one working, you can examine the Request object to determine what Url was used exactly and extract the parameters any way you like

Basic
  • 26,321
  • 24
  • 115
  • 201