ASP.NET MVC routing using an identifier or action name

I have an ASP.Net application that has an area called Clients. In this area, there is a controller with the same name using a single Index method.

I defined the following route:

context.MapRoute(null,
    "Customers/{controller}/{action}",
    new { controller = "customers", action = "Index" }
);

This allows me to go to the following URL to go to the index method on my client controller.

MYDOMAIN / Customers

In my client area, I also have another controller called a product. This has several methods that allow me to work with product objects (mostly automatically generated by Visual Studio at the moment).

With my current route, I can go to the product controller using the following URL:

MyDomain// ( ) MyDomain/// ( ). MyDomain///? Id = 1234 ( 1234)

, , - URL-, :

MyDomain///1234

, :

context.MapRoute(null,
    "Customers/Products/{id}",
    new { controller = "Products", action = "Details" }
    );

. , , .

. URL

MyDomain///

:

'id' of non-nullable type 'System.Int32' 'System.Web.Mvc.ViewResult (Int32)

, , URL- .

:

context.MapRoute(null,
    "Customers/Products/{id}",
    new { controller = "Products", action = "Details", id = UrlParameter.Optional }
    );

.

- , , , ? :

  • "", , URL- "MyDomain/Customers"
  • , , URL- "MyDomain/Customers/Products/1234".
  • , , URL- "MyDomain/Customers/Products/Create"
+5
4

int, :

context.MapRoute(null,
                 "Customers/Products/{id}",
                 new {controller = "Products", action = "Details", id = UrlParameter.Optional},
                 new {id = @"\d+"} // Constraint to only allow numbers
                );
+4

- :

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
        "Products_Details",
        "Customers/Products/{id}",
        new { controller="Products", action="Details" },
        new { id = @"\d+" }
    );

    context.MapRoute(
        "Customers_default",
        "Customers/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

: .

: , ( MapRoute). \d+, ​​ . :

Customers\Products (index)
Customers\Products\1234 (details of 1234)
Customers\Products\Create (create page for products)
Customers\Products\Edit\1234 (edit page for 1234)
+1

If you want to keep existing routes, try this:

context.MapRoute(null,
     "Customers/Products/{id}", // id is optional, so Customers/Products might reasonably return all products
     new { controller = "Products", action = "Details", id = UrlParameter.Optional },
     new {id = @"\d+"} // must be numeric if present
);

context.MapRoute(null,
     "Customers/Products/{action}/{id}", // Id is optional, so this needs an action.
     new { controller = "Products", action = "Details", id = UrlParameter.Optional }
     // maybe the id is not numeric for all actions? I don't know so I won't constrain it.
);

context.MapRoute(null,
     "Customers/{controller}/{action}", // Default Customers route
     new { controller = "customers", action = "Index" }
);

The first two have a URL, for example /Customers/Products/..., with one or two other parts, and the last with anything else, starting with/Customers/

0
source

try it

    context.MapRoute(
                    "Customers_Products",
                    "Customers/Products/{productId}",
                    new { controller = "Products", action = "Details", productId= UrlParameter.Optional }
                );

    context.MapRoute(null,
                    "Customers/{controller}/{action}",
                     new { controller = "customers", action = "Index" }
);
0
source

All Articles