views:

55

answers:

2

I'd like to have routes set up as follows:

"xyz/" maps to one action method with no parameters, but "xyz/{username}" maps to a different action (in the same controller or not, doesn't matter) that takes an argument called username of type string. Here are my routes so far:

routes.MapRoute(
    "Me",
    "Profile",
    new { controller = "Profile", action = "Me" }
);

routes.MapRoute(
    "Profile",
    "Profile/{username}",
    new { controller = "Profile", action = "Index" }
);

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

Currently, if I navigate to "/Profile/someuser", the Index action in the Profile controller is hit as expected. But if I navigate to "/Profile/" or "/Profile", I get 404. What gives?

+3  A: 

It works fine for me. I suppose you do not have a Me action method in your Profile controller?

I tried with the following routes:

routes.MapRoute(
    "Me",
    "Profile",
    new { controller = "Home", action = "Me" }
);

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

With the Index and Me action methods defined like this :

public ActionResult Index(string username) {
   ViewData["Message"] = username;
   return View();
}

public ActionResult Me() {
   ViewData["Message"] = "this is me!";
   return View( "Index" );
}

I used the default Home/Index view.

Bertrand Marron
+1 for the non-repro. Commenting the `Me()` results in a 404. You're likely right on the lack of a `Me()` in the controller.
p.campbell
A: 

Wow, sorry, I accidentally had the Me() method set to private. That took way too long to spot. I hate bugs like that, you pull your hair out for hours and it turns out to be drop-dead trivial. I actually suspected it would be something dumb like that but I was at wit's end.

gzak
@gzak: this should be a comment, not an answer. Also, please accept @Bertrand's answer.
Robaticus