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?

2
  • Can you share what your routes look like right now? Commented Dec 27, 2012 at 21:14
  • Use Glimpse getglimpse.com and see what is wrong with your route. Commented Dec 27, 2012 at 21:19

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Action filter didn't work - never gets to a controller action to execute the filter.
You will need to do a Global Action Filter. This will intercept on all requests.
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?

4 Comments

Yeah, the idea is to have something very "rememberable" by normal humans and that the client can advertise to their clients.
You said the default route keeps catching it - do you actually need the default route for something else?
Yes, I am using the default route for just about everything else.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.