Search code examples
c#asp.netvirtual-directory

asp.net make virtual directory on database entry


I have a URL foo.com/page.aspx?id=1 and the ID is equal to a record in the database.

I would like the website to create a virtual directory as foo.com/1/ and have it display the same thing after the record is created in the database.

How do I create a virtual directory or is there a way to translate it on the server?


Solution

  • I would recommend the IIS url rewrite module. That way the page request foo.com/1 is translated to foo.com/page.aspx?id=1.

    <rewrite>
      <rules>
        <rule name="pages" stopProcessing="true">
          <match url="^([0-9/]+)" ignoreCase="true"/>
          <action type="Rewrite" url="page.aspx?id={R:1}"/>
        </rule>
      </rules>
    </rewrite>
    

    Then you can just fetch the id with string id = Request.QueryString["id"].ToString(); on page.aspx and show the corresponding contents from the database.