Reputation: 26018
I want to display a list of mountain biking trails per province or per area. A province consists of many areas.
I am trying to display the following 2 URLs:
www.mywebsite.com/paarl-trails (area trails)
www.mywebsite.com/western-cape-trails (province trails)
I have the following route configuration (in this order):
routes.MapRoute(
name: "ProvinceDefault",
url: "{seoProvinceName}-trails",
defaults: new { controller = "Province", action = "Index" },
namespaces: new[] { "MyProject.WebUI.Controllers" }
);
routes.MapRoute(
name: "AreaDefault",
url: "{seoAreaName}-trails",
defaults: new { controller = "Area", action = "Index" },
namespaces: new[] { "MyProject.WebUI.Controllers" }
);
This is how I create the links in the view:
<a href="@Url.RouteUrl("AreaDefault", new { seoAreaName = trail.Area.SeoName })">@trail.Area.Name</a>,
<a href="@Url.RouteUrl("ProvinceDefault", new { seoProvinceName = trail.Area.Province.SeoName })">@trail.Area.Province.Name</a>
The links display correctly but both are redirected to the province controller's Index method. I changed the 2 route configurations around but then they both go to the Index method of the area controller.
I want www.mywebsite.com/paarl-trails
to go to the Index method of the area controller and I want www.mywebsite.com/western-cape-trails
to go to the Index method of the province controller.
How do I get this right?
Upvotes: 0
Views: 179
Reputation: 13767
I agree with the previous answer because ASP.NET MVC routing engine has no way to differentiate both routes. What you can do is add the area name before the SEOProvinceName. That way, routes would like this:
In order to make this work, the routes should be something like this:
routes.MapRoute(
name: "ProvinceDefault",
url: "{seoAreaName}/{seoProvinceName}-province-trails",
defaults: new { controller = "Province", action = "Index" },
namespaces: new[] { "MyProject.WebUI.Controllers" }
);
routes.MapRoute(
name: "AreaDefault",
url: "{seoAreaName}-area-trails",
defaults: new { controller = "Area", action = "Index" },
namespaces: new[] { "MyProject.WebUI.Controllers" }
);
I hope it helps.
Upvotes: 1
Reputation: 785
MVC Routing doesn't work that way. When an URL is requested from the server, the routing engine picks the first route that matches the URL, in the order you've defined them. The routing system can't differentiate between the two routes you've defined because to it they look the same: something followed by -trails
.
You would have to change the URLs and their routes to get the routing engine to handle them correctly, something like this:
URLs:
Routes:
routes.MapRoute(
name: "ProvinceDefault",
url: "{seoProvinceName}-province-trails",
defaults: new { controller = "Province", action = "Index" },
namespaces: new[] { "MyProject.WebUI.Controllers" }
);
routes.MapRoute(
name: "AreaDefault",
url: "{seoAreaName}-area-trails",
defaults: new { controller = "Area", action = "Index" },
namespaces: new[] { "MyProject.WebUI.Controllers" }
);
Upvotes: 0