1

So, I've read through tutorials and books about MVC routing as well as played with it on my projects and come to a pretty solid understanding of how to use it to accomplish what I want to with it.

But, I'm up against something I can't quite figure out yet.

What I want to accomplish is a unique url for each client that doesn't look like "http://mysite.com/client/1". This url would take the browser to the Client Controller, Index action, ClientId = 1...obviously.

What I'd like to do is have a URL like "http://mysite.com/Acme" that would do a database lookup to figure out which client has the unique name of "Acme", and then redirect the request to the Client Controller, Index view and set the ClientId to whatever it is on the client with the name 'Acme'.

The default route keeps catching it and can't handle it.

Any ideas?

coach_rob
  • 877
  • 13
  • 28

2 Answers2

1

I recommend using an Global Action Filter to accomplish this or you can create a route with a static path that will route to your lookup controller (e.g., /lookup/{companyname} will route to your database lookup controller).

Jonathan Harrison
  • 883
  • 1
  • 6
  • 13
1

How about "http://www.mysite.com/Clients/{ClientName}"

routes.MapRoute(null, "Clients/{ClientName}", new{controller = "Clients", action = "Index"};

public class ClientsController : Controller
{
    public ActionResult Index(string clientName)
    {
        var id = Db.GetClientIdBy(clientName);

        // do your redirect...
    }
}    

Or have I missed the point?

Greg Smith
  • 2,449
  • 2
  • 24
  • 37
  • Yeah, the idea is to have something very "rememberable" by normal humans and that the client can advertise to their clients. – coach_rob Dec 27 '12 at 21:41
  • You said the default route keeps catching it - do you actually need the default route for something else? – Greg Smith Dec 27 '12 at 22:09
  • Yes, I am using the default route for just about everything else. – coach_rob Dec 28 '12 at 18:36
  • I think you're in trouble then. You'll need to catch a request in some kind of custom controller (or global filter), decide whether it's a "client" request or not, presumably by hitting the DB (on EVERY request - bad) then forward on to the actual controller/action. If it were me, i'd rethink the routing scheme to accommodate the requirement: the routing engine is enormously flexible and powerful, and designed for the exact purpose you've described. – Greg Smith Dec 28 '12 at 18:43