layer:11

an addiction to or an obsession with acquiring, manipulating, and sharing information

URL Mapping

Here’s full, working and as simple as possible example on how to get pretty URLs with .NET/Mono.

First, here’s all the code:

 1 using System;
 2 using System.Web;
 3 using System.Web.Routing;
 4 
 5 
 6 class RouteHandler : IRouteHandler {
 7 
 8   Type PageType;
 9 
10   public RouteHandler(Type pageType) {
11     PageType = pageType;
12   }
13 
14   public IHttpHandler GetHttpHandler(RequestContext requestContext) {
15     var routeData = requestContext.RouteData.Values.Values;
16     string[] args = new string[routeData.Count];
17     routeData.CopyTo(args, 0);
18     Page page = (Page)Activator.CreateInstance(PageType, args);
19     return page;
20   }
21 
22 }
23 
24 
25 public class Global : HttpApplication {
26 
27   protected void Application_Start() {
28     RouteCollection r = RouteTable.Routes;
29     r.Add(new Route("foo", new RouteHandler(typeof(Foo))));
30     r.Add(new Route("bar/{id}", new RouteHandler(typeof(Bar))));
31   }
32 
33 }
34 
35 
36 class Page : System.Web.UI.Page {
37 
38   override protected void OnLoadComplete(EventArgs e) {
39     if (Request.HttpMethod == "POST") {
40       Post();
41     } else {
42       Get();
43     }
44   }
45 
46   virtual protected void Get() {
47     throw new NotImplementedException();
48   }
49 
50   virtual protected void Post() {
51     throw new NotImplementedException();
52   }
53 
54 }
55 
56 
57 class Foo : Page {
58 
59   override protected void Get() {
60     Response.Write("FOO!");
61   }
62 
63 }
64 
65 
66 class Bar : Page {
67 
68   string Id;
69 
70   public Bar(string id) {
71     Id = id;
72   }
73 
74   override protected void Get() {
75     Response.Write("BAR" + Id + "!");
76   }
77 
78 }

Save that as global.cs and compile it with this, if you’re using Mono:

1 gmcs -debug+ -r:System.Web,System.Web.Routing -t:library global.cs

Or this, if you’re using .NET:

1 csc /debug+ /r:System.Web.Routing.dll /t:library global.cs

Drop the resulting global.dll in Bin directory on the web server. And to wire it up, you’ll need this global.asax:

1 <%@ Inherits="Global" %>

Also, your web.config should look something like this:

 1 <?xml version="1.0"?>
 2 <configuration>
 3   <system.web>
 4     <customErrors mode="Off" />
 5     <httpModules>
 6       <clear />
 7       <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
 8     </httpModules>
 9   </system.web>
10 </configuration>

And that should be it! Try visiting /foo and /bar/123 on the web server. I successfully tested this with xsp2, lighttpd and IIS 6.0, just remember that, for this to work, web server needs to send all requests to CGI/ISAPI module, xsp2 always does this, in lighttpd, simply send all requests to fastcgi-mono-server, in IIS, send everything to aspnet_isapi.dll using “wildcard application maps” setting.

| Archive | RSS | E-mail | Twitter | Alvis Mikovs