1
votes

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?

2
Can you share what your routes look like right now?Erik W
Use Glimpse getglimpse.com and see what is wrong with your route.Matija Grcic

2 Answers

1
votes

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).

1
votes

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?