12

I there a simple way when using ASP.NET 4.0 routing with Web Forms to produce a route that will act as some kind of wildcard?

It seems to me that within WebForms, you have to specify a route for every page - I am looking for some kind of generic route that can be used where nothing specific is required, perhaps mapping directly from path to path so...

http://somedomain.com/folder1/folder2/page would possibly map to folder1/folder2/page.aspx

Any suggestions?

Thanks

skaffman
  • 398,947
  • 96
  • 818
  • 769
Martin Robins
  • 6,033
  • 10
  • 58
  • 95

2 Answers2

24

You can match all remaining routes like this:

routes.MapPageRoute("defaultRoute", "{*value}", "~/Missing.aspx");

In this case, we know all routes, and want to send anything else to a "missing"/404 page. Just be sure to put this as the last route, since it is a wildcard and will catch everything.

Alternatively you could register a route the same way, but internally does mapping to a page, like this:

routes.Add(new Route("{*value}", new DefaultRouteHandler()));

That handler class would do your wildcard mapping, something like this:

public class DefaultRouteHandler : IRouteHandler
{
  public IHttpHandler GetHttpHandler(RequestContext requestContext)
  { 
    //Url mapping however you want here:
    var pageUrl = requestContext.RouteData.Route.Url + ".aspx";

    var page = BuildManager.CreateInstanceFromVirtualPath(pageUrl, typeof(Page)) 
               as IHttpHandler;
    if (page != null)
    {
      //Set the <form>'s postback url to the route
      var webForm = page as Page;
      if (webForm != null) 
         webForm.Load += delegate { webForm.Form.Action = 
                                    requestContext.HttpContext.Request.RawUrl; };
    }
    return page;
  }
}

This is broken a bit in odd places to prevent horizontal scrolling, but you get the overall point. Again, make sure this is the last route, otherwise it'll handle all your routes.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Is it possible then to do something like: Dim prospect As String = Page.RouteData.Values("*value") I know it is when it isn't a wildcard, but what about a wildcard? – Dave Mackey May 10 '12 at 14:57
  • 1
    I am getting following error. What am I missing? (.Net 4.5) System.Web.Routing.RouteBase' does not contain a definition for 'Url' and no extension method 'Url' accepting a first argument of type 'System.Web.Routing.RouteBase' could be found (are you missing a using directive or an assembly reference?) – HGMamaci Feb 16 '14 at 21:38
0

Additionally - Keep in mind that you need to add an exception for the .axd files in your Global.asax file if there are validation controls in your web app:

http://basgun.wordpress.com/2010/10/25/getting-syntax-error-in-asp-net-routing-due-to-webresource-axd/

Otherwise, you will keep getting a syntax error because the routing picks up the .axd files and not properly loads the JavaScript files needed for the validation controls.

ncakmak
  • 4,014
  • 5
  • 20
  • 17