Tim Morford Tim Morford - 9 months ago 65
ASP.NET (C#) Question

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?

Answer Source

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.

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

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.