Ktt
Ktt

Reputation: 469

Keep getting 404 eventhough I mapped my routes

I have been dealing with some issues about routes. I have defined the routes but I keep getting 404. Here are the routes :

routes.MapRoute(
    name: "Default",
    url: "{controller}",
    defaults: new { controller = "Login", action = "Login" }
);

routes.MapRoute(
    name: "Home",
    url: "{controller}/{Date}",
    defaults: new { controller = "Home", action = "Home", Date = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Calendar",
    url: "{controller}/{action}",
    defaults: new { controller = "Calendar", action = "Index" }
);

routes.MapRoute(
    name: "Act",
    url: "{controller}",
    defaults: new { controller = "Act", action = "New" }
);

localhost:51081/login works! 
localhost:51081/Home/25.04.2013 works! 
localhost:51081/act doesnt work! 
localhost:51081/calendar/index doesnt work!

Here "login" and "home" works but "calendar" and "act" doesnt. When I move "calendar" mapping to the top then "home" mapping doesnt work. how do you map your pages?

Basically I dont want action name to appear on the url ex : http://localhost:51081/Home/Home/25.04.2013. I want to see it like http://localhost:51081/Home/25.04.2013 or http://localhost:51081/calendar

Upvotes: 2

Views: 110

Answers (3)

Ruben-J
Ruben-J

Reputation: 2693

Like @MarcGravell says: you only add special rules for the exceptions

In your case routes Calendar and Home are the same. You can map your routes more specific by replacing {controller} with the Home, cause that route isn't that dynamic and is really an exception(it ignores the action)

  routes.MapRoute(
            name: "Home",
            url: "Home/{Date}",
            defaults: new { controller = "Home", action = "Home", Date = UrlParameter.Optional }
        );

Act is the same as calendar so you don't need two routes for those. Just call Act/New instead of only Act.

For the Default use:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}",
    defaults: new { controller = "Login", action = "Login" }
);

And put it at the bottom of your routes off course.

Upvotes: 3

Marc Gravell
Marc Gravell

Reputation: 1063884

routes.MapRoute(
            name: "Default",
            url: "{controller}",
            defaults: new { controller = "Login", action = "Login" }
        );

This defines a route that matches / and /anything; the / will try to use LoginController.Login, and /anything will try to use anythingController.Login. Note that at no point does this route allow it to pick up any "action" other than Login.

If you trow all of those away, and use something like:

routes.MapRoute(
         name: "Default",
         url: "{controller}/{action}",
         defaults: new { controller = "Home", action = "Index" }
         );

then that will match any of /, /anything and /anything/anythingelse.

  • / will map to HomeController.Index
  • /anything will map to anythingController.Index
  • /anything/anythingelse will map to anythingController.anythingelse

Hopefully that explains how the mapping works in terms of defaults.

If you have any specific routes, they should be added before this blanket default.

Upvotes: 2

David Hoerster
David Hoerster

Reputation: 28701

Remember that asp.net routes are evaluated in the order in which you add them to the MapRoute table.

Your "default" and "act" routes are the same, since they have the same pattern. "Act" will probably never get hit.

Also, your "default" route is pretty generic, and most requests will satisfy it. You should add your routes in order of most specific (e.g. hard-coded routes) to least specific (e.g. all placeholders).

So if I have a request of foo/bar, it will fall to your "default" route since "foo" will be interpreted as the controller -- it's then going to look for a resource of "bar" which probably doesn't exist. So you'll get a 404.

Your "home" and "calendar" routes are also the same pattern, so only one will get hit (which will be the first defined).

Make your routes more specific, and define them from most specific to least.

Good luck!

Upvotes: 1

Related Questions