Two MapRoutes with different data types

I need to have the following C # MVC calls, for example:

/ SomeController / ActionOne / 1/2/3 /
SomeController / ActionTwo / 1/2 / astring

Here are my route maps:

routes.MapRoute (
                name: "One",
                url: "SomeController/{action}/{intId1}/{intId2}/{intId3}",
                defaults: new { controller = "SomeController", action = "ActionOne" }
);

routes.MapRoute (
                name: "Two",
                url: "SomeController/{action}/{intId1}/{intId2}/{string1}",
                defaults: new { controller = "SomeController", action = "ActionTwo" }
);

and the controller will look something like this:

public ActionResult ActionOne ( int intId1, int intId2, int intId3 )
            { ... }
public ActionResult ActionTwo ( int intId1, int intId2, string string1 )
            { ... }

When I use URL / SomeController / ActionTwo / 1/2 / astring , it gives

The parameter dictionary contains a null entry for the parameter ....

I would like to avoid using an unused parameter just to bypass a routing rule such as / SomeController / ActionTwo / 1/2 // astring :

public ActionResult ActionOne ( int intId1, int intId2, int? intId3, string? string1 )
            { ... }
public ActionResult ActionTwo ( int intId1, int intId2, int? intId3, string? string1 )
            { ... }
+4
source share
1 answer

, Route . , URL- :

[Route("SomeController/{action}/{intId1}/{intId2}/{intId3:int}]

( Route URL- , , )

( -Api, ).

URL- : constraints MapRoute.

routes.MapRoute(
name: "One",
url: "SomeController/{action}/{intId1}/{intId2}/{intId3}",
defaults: new { controller = "SomeController", action = "ActionOne" }
constraints: new{intId3=@"\d+"} 
);

Regex.

+2

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


All Articles