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.

3 Answers 3

4

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.

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

Comments

1

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

3 Comments

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

Steps to solve Right click on the project Go to web tab of the page. My Start URL was found to be empty I copied what in Project url of Servers section Pasted at Start Url

It worked.

1 Comment

This is 10-years old question with answers. Question is related to configuration of routes, controllers and actions. It is not clear to me how your answer is related to the question - I do not see in your answer any mention of controllers and actions. If you think that you can add good up-to-date answer then please add explanation to your answer.

Your Answer

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