Consider an ASP.NET MVC application with two models: let's say Company
and Person
. Each company has a list of persons. Each person belongs to one company only.
If you set up the model and use Visual Studio to generate the controllers/views, you get the ability to edit the companies at /Company/{id}
etc. and the ability to edit the persons at /Person/{id}
etc.
But I want it to be such that you can only add a person inside a company, i.e. you would edit the persons at /Company/{id}/Persons/{id}
.
How can I set up this sort of routing in ASP.NET MVC 5?
EDIT:
So I did this in my routes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CompanyPerson",
url: "Company/{CompanyId}/Person/{PersonId}/{action}",
defaults: new { controller = "Person", action = "Index", PersonId = UrlParameter.Optional }
);
routes.MapRoute(
name: "Company",
url: "Company/{id}/{action}",
defaults: new { controller = "Company", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/",
defaults: new { controller = "Home", action = "Index" }
);
but it still isn't working. If I go to /Company/7/Person
I get an index of persons, but /Company/7/Person/Create
just gives the same index, and the "Create New" link points to /Person/Create
instead of /Company/7/Person/Create
Is there just a way to set up all the routes explicitly, like Node or most other MVC frameworks?