pete
pete

Reputation: 55

ASP.NET MVC 3 - Area URL Routing Problem

I have an ASP.Net MVC 3 application. With 2 Areas:

In the Management Area I have a ManagementController and a PropertyController. The ManagementController does nothing, it only has a simple Index ActionResult than return a view.

The PropertyController has an Index ActionResult that returns a view with a list of properties as well as an Edit ActionResult that takes a propertyId as parameter. In the Property Index view i have a grid with the list of properties and an option to edit the property with the following code:

@Html.ActionLink("Edit", "Edit","Property", new { id = item.PropertyId })

In theory this should redirect to the Edit ActionResult of my Property Controller,however it redirects to the Index ActionResult of my ManagementController. My ManagementAreaRegistration file looks like this:

context.MapRoute(null, "management", new { controller = "management", action = "Index" });
context.MapRoute(null, "management/properties", new { controller = "Property", action = "Index" });
context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });

And my global.asax's RegisterRoutes like this:

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

What am I missing, why would it redirect to the wrong controller and action? Thanks in Advance!

Upvotes: 3

Views: 7314

Answers (4)

NoWar
NoWar

Reputation: 37642

From any Area to Home do following

Url.Action("Details", "User", new { @id = entityId, area = "" })

From Home to any Area

Url.Action("Details", "Order", new { @id = entityId, area = "Admin" })

Upvotes: 0

Tristan Warner-Smith
Tristan Warner-Smith

Reputation: 9781

I'd suggest using constraints. For example my default route in the Global.asax is as follows:

routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { typeof(MyProject.Controllers.HomeController).Namespace });

Then in my area registration:

context.MapRoute(
"Search_default",
"Search/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { typeof(MyProject.Areas.Search.Controllers.HomeController).Namespace }
            );

This means I get {AppName}/ as well as {AppName}/Search/Home and both work a treat.

Upvotes: 1

frennky
frennky

Reputation: 14004

In your route you defined a parameter called propertyId not id:

context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });

Try this:

@Html.ActionLink("Edit", "Edit","Property", new { propertyId = item.PropertyId })

Upvotes: 3

MrGrigg
MrGrigg

Reputation: 1732

You might need to specify the area in your route values parameter:

@Html.ActionLink("Edit", "Edit", "Property", new { id = item.PropertyID, area = "Management" }, new { })

Based on the constructor used for this call, you need to specify the last parameter, htmlAttributes, which, for your purposes, would be empty.

Upvotes: 6

Related Questions