Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friendly URLs for ASP.NET

Tags:

Python frameworks always provide ways to handle URLs that convey the data of the request in an elegant way, like for example http://somewhere.overtherainbow.com/userid/123424/

I want you to notice the ending path /userid/123424/

How do you do this in ASP.NET?

like image 365
minty Avatar asked Sep 05 '08 06:09

minty


People also ask

What is a friendly URL example?

The benefit of creating a friendly URL is that they are easier to remember and contain key words about the web page. Example: Original URL: https://portal.ct.gov/Services/Education/Higher-Education/Higher-Education-Information-and-Resources. Friendly URL: https://portal.ct.gov/SDE-HigherEd.

How do I disable friendly URLs?

If you want to enable Human-Friendly URLs, ensure that the urlFormat="humanfriendly" does exist as listed above. If you want to disable Human-Friendly URLs, ensure that urlFormat="humanfriendly" is removed from that section of the web.


1 Answers

This example uses ASP.NET Routing to implement friendly URLs.

Examples of the mappings that the application handles are:

http://samplesite/userid/1234 - http://samplesite/users.aspx?userid=1234
http://samplesite/userid/1235 - http://samplesite/users.aspx?userid=1235

This example uses querystrings and avoids any requirement to modify the code on the aspx page.

Step 1 - add the necessary entries to web.config

<system.web> <compilation debug="true">         <assemblies>             …             <add assembly="System.Web.Routing, Version=3.5.0.0,    Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>         </assemblies>     </compilation> …     <httpModules>     …         <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />         </httpModules> </system.web> <system.webServer>     …     <modules>         …         <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>     </modules>     <handlers …            <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler,                 System.Web, Version=2.0.0.0, Culture=neutral,              PublicKeyToken=b03f5f7f11d50a3a"/>     </handlers> </system.webServer> 

Step 2 - add a routing table in global.asax

Define the mapping from the friendly URL to the aspx page, saving the requested userid for later use.

void Application_Start(object sender, EventArgs e) {     RegisterRoutes(RouteTable.Routes); }  public static void RegisterRoutes(RouteCollection routes) {     routes.Add("UseridRoute", new Route     (        "userid/{userid}",        new CustomRouteHandler("~/users.aspx")     )); } 

Step 3 - implement the route handler

Add the querystring to the current context before the routing takes place.

using System.Web.Compilation; using System.Web.UI; using System.Web; using System.Web.Routing;  public class CustomRouteHandler : IRouteHandler {     public CustomRouteHandler(string virtualPath)     {         this.VirtualPath = virtualPath;     }      public string VirtualPath { get; private set; }      public IHttpHandler GetHttpHandler(RequestContext           requestContext)     {         // Add the querystring to the URL in the current context         string queryString = "?userid=" + requestContext.RouteData.Values["userid"];         HttpContext.Current.RewritePath(           string.Concat(           VirtualPath,           queryString));           var page = BuildManager.CreateInstanceFromVirtualPath              (VirtualPath, typeof(Page)) as IHttpHandler;         return page;     } } 

Code from users.aspx

The code on the aspx page for reference.

protected void Page_Load(object sender, EventArgs e) {     string id = Page.Request.QueryString["userid"];     switch (id)     {         case "1234":             lblUserId.Text = id;             lblUserName.Text = "Bill";             break;         case "1235":             lblUserId.Text = id;             lblUserName.Text = "Claire";             break;         case "1236":             lblUserId.Text = id;             lblUserName.Text = "David";             break;         default:             lblUserId.Text = "0000";             lblUserName.Text = "Unknown";             break; } 
like image 191
Dominic Betts Avatar answered Sep 20 '22 17:09

Dominic Betts