views:

173

answers:

2

How do you set up the routing to handle http://mysite.com/Foo?

Where foo can be any value. I want it to go to /Home/Action/Id where foo is the id.

These are the mappings I have tried:

        routes.MapRoute("Catchall", "{*catchall}", 
           new { controller = "Home", action = "Index", id=""});

        routes.MapRoute("ByKey", "{id}", 
            new { controller = "Home", action = "Index" id=""});

They both generated a 404.

Thanks for any help.

+1  A: 

Your second route is correct. It should work. Maybe you have another routes above? Debug your routes with Phil Haack's ASP.NET Routing Debugger

eu-ge-ne
I believe unit tests does the trick better. Have you tried MvcContrib routing test helpers? :)
Arnis L.
Something like `"~/foo".Route().ShouldMapTo<HomeController>(x => x.Index("foo"));` would be interesting. You should post an answer too
eu-ge-ne
But I dont understand how it helps David if this test fails?
eu-ge-ne
+3  A: 

Try this (note you had a missing comma in your original post):

routes.MapRoute("ByKey", "{id}",
     new { controller = "Home", action = "Index", id=""});

I would, however, make it a bit more explanatory to prevent clashes later, even if it means a bit longer URIs:

routes.MapRoute("ByKey", "ByKey/{id}",
     new { controller = "Home", action = "Index", id=""});

And place this as the first MapRoute command. Order does matter there, and the first route you add is the first route for a URL to be tested with.

synhershko