views:

31

answers:

2

i want to build a route entry for a url http://example.com/foo which should map to Index(string foo) action method of a controller named User.

At the same time the default Home Controller should not be affected and should work normally

any help is greatly appreciated

Thanks

A: 

You could try putting this before the default route:

routes.MapRoute(
    "FooRoute",
    "{foo}",
    new
    {
        controller = "Home",
        action = "Index",
        foo = UrlParameter.Optional
    }
);
Darin Dimitrov
A: 

Thanks Darin Dimitrov for your instant reply,

I am defining my routes in the following order

routes.MapRoute(
                "FooRoute",
                "{foo}",
                 new { controller = "Test", action = "Index", foo = "" }
             );
routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

Now a request for example.com/foo is received by Index method of test Controller, but a request like example.com is also received by the same action method, which is not what i want , I want the default home controller should be kept intact by that i mean any request like example.com , example.com/home/, example.com/home/index/, example.com/ should all be served by the index action method of the home controller.

More over i am not able to receive foo as a query string when i make the request like example.com/foo , my action method index of the test controller looks like this

public ActionResult Index(string test)
        {
            ViewData["Message"] = test;

            return View();
        }

Thank

* sorry for not being able to show the links as i have been denied by the stackoverflow guys and they want me to earn 10 more points to do that.

Mvc Programmer