korall
korall

Reputation: 45

.NET MVC 4 route map and Html.Action

I got struggled in such a problem that when i have a route map configuration like

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

I got "hxxp://site.com/Merchandise/Controller/Action/1" from @Html.Action("Action","Controller", new { Id = "1"}) where "hxxp://site.com/Controller/Action/1" was expected.

If route map configured to

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

then I got 404 when trying an Url like "hxxp://site.com/Merchandise/Controller" with Merchandise being not a Controller ("hxxp://site.com/Merchandise/Controller/Action/1" is OK BTW). How can i solve this contradiction? What i want is that "Merchandise" here act as an role of category but not a controller.

Upvotes: 3

Views: 416

Answers (1)

ckv
ckv

Reputation: 10830

Merchandise in your case can be called an area. Refer Areas in ASP.NET MVC As per design of routes if you have any custom routes that should be defined before the default route. Because that is the order in which the urls are decoded to routes. So in your first case you have your custom route defined before the default route and hence it works correctly while in the second case the default route is defined first.

Upvotes: 2

Related Questions