Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC Routing - how do I match the whole URL?

I'm trying to create a catch-all route to track when an affiliate string is in the URL. An affilite code is marked by an x followed by an int, and will only appear at the end of a URL (but before the query string).

The idea is that I will extract the affiliate ID, do some logging, then do a 301 to the same request without the affiliate ID.

For example:

http://www.domain.com/x32
http://www.domain.com/x32/
http://www.domain.com/path/to/something/x32
http://www.domain.com/x32?query=string
http://www.domain.com/x32/?query=string
http://www.domain.com/path/to/something/x32?query=string
http://www.domain.com/path/to/something/x32/?query=string

I have this route

routes.Add(new Route("{url}/x{affiliateExternalId}", new MvcRouteHandler())
{
     Defaults = new RouteValueDictionary(
      new { controller = "Home", action = "LogCookieAndRedirect" }
     ),
     Constraints = new RouteValueDictionary(new { affiliateExternalId = @"\d{1,6}" })
});

Which only matches

http://www.domain.com/path/x32
http://www.domain.com/path/x32/

What do I need to do to match everything and pass the query string to the controller? There is a * operator that I suspect I should use, but I can't get it to do what I need.

like image 602
Mr. Flibble Avatar asked Oct 24 '09 19:10

Mr. Flibble


People also ask

How are URL patterns mapped to a handler in ASP.NET MVC?

Typical URL Patterns in MVC Applications URL patterns for routes in MVC Applications typically include {controller} and {action} placeholders. When a request is received, it is routed to the UrlRoutingModule object and then to the MvcHandler HTTP handler.

How do I change the default URL in MVC?

You can set up a default route: routes. MapRoute( "Default", // Route name "", // URL with parameters new { controller = "Home", action = "Index"} // Parameter defaults ); Just change the Controller/Action names to your desired default.

How do I map multiple URLs to the same action in MVC?

Yes, We can use multiple URLs to the same action with the use of a routing table. foreach(string url in urls)routes. MapRoute("RouteName-" + url, url, new { controller = "Page", action = "Index" });

How do I route a URL?

The handler can be a physical file, such as an . aspx file in a Web Forms application. A handler can also be a class that processes the request. To define a route, you create an instance of the Route class by specifying the URL pattern, the handler, and optionally a name for the route.


1 Answers

{*url} will match the whole path rather than just one segment. However, since it matches the whole path, you can't then match the affiliate ID on the end. The route will match every request. You can get around that by adding a route constraint to check that url has an affiliate ID at the end:

routes.MapRoute(
    "WithAffiliate",
    "{*url}",
    new { controller="Home", action="LogCookieAndRedirect" },
    new { url = @"/x[0-9]+$" }
 );

Your action will then need to parse the affiliate ID from the url parameter itself. If you have the option to modify the URL structure, it would be possible to match the ID if it were at the start of the path:

routes.MapRoute(
    "WithAffiliate",
    "x{affiliateExternalId}/{*url}",
    new { controller="Home", action="LogCookieAndRedirect" }
 );
like image 109
stevemegson Avatar answered Dec 20 '22 00:12

stevemegson