Asp.Net MVC Routes - handle multiple routes with the same signature?

I am interested to know how people deal with the following situation.

Suppose we have a DataField, and each DataField can have an unlimited number of DataValues

We have 2 controllers for handling manipulations with these objects

  • DataFieldController
  • DataValueContoller

Now, if we need to add a new DataValue, we need to know the identifier CustomDataField. The following URL will be used:

/CustomDataValue/Add/1 

1 = DataField ID

However, since the ASp.Net MVC mechanism associates the parameter name with the model (IE in the case below. My DatValeu object will have its identifier replaced when I really try to go through FieldID)

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

? , , .

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

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

, , . , , :/String/String/Int

==========================

?

  • /DataValue//{DataFieldID}
  • /DataValue//{ID}
  • /DataValue//{DataFieldID}

3 ?

+3
1

, :

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

, URL-, "CustomDataValue", . , . , . , URL- .

, DataValueController URL-, http://domain.com/CustomDataValue/Add/23. :

routes.MapRoute(
    "CustomData", // Route name
    "CustomDataValue/{action}/{fieldid}", // URL with parameters
    new { controller = "DataValue", action = "List", fieldid = UrlParameter.Optional } // Parameter defaults
);

, DataValueController List/Add/Edit.

+5

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


All Articles