ASP.NET MVC 3 - URL Routing Issue

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

  • Auth - handles all authentication, etc.
  • Management - Real Estate Management

In the management area, I have ManagementController and PropertyController. ManagementController does nothing, it has a simple ActionResult pointer, which returns a view.

The PropertyController has an ActionResult pointer, which returns a view with a list of properties, as well as an Edit ActionResult, which takes propertyId as. In the property index view, I have a grid with a list of properties and the ability to change a property with the following code:

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

In theory, this should be redirected to the Edit ActionResult of my property controller, however it will redirect 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 RegisterRoutes:

            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 is it redirecting to the wrong controller and action? Thanks at Advance!

+3
source share
4 answers

In your route, you have defined the propertyId not id parameter:

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

Try the following:

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

You may need to specify an area in the route parameter 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 htmlAttributes parameter, which will be empty for your purposes.

+6
source

. , Global.asax :

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

:

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 }
            );

, {AppName}/, {AppName}/Search/Home .

+1
source

From any area to home, follow these steps:

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

From home to any area

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

Source: https://habr.com/ru/post/1794440/


All Articles