开发者

ASP.NET MVC Routing - Keeping up with parameters through redirects

开发者 https://www.devze.com 2023-04-09 23:51 出处:网络
I am working on my first real ASP.NET MVC project and I need a little advice. So, let\'s say that my app has an admin section where you can create companies, departments, and users within each depart

I am working on my first real ASP.NET MVC project and I need a little advice.

So, let's say that my app has an admin section where you can create companies, departments, and users within each department.

Company -> Department -> User

Right now my urls are looking like this:

Companies/Index
Departments/Index?companyId=1
Users/Index?companyId=1&departmentId=2

It's getting complicated because if I have to redirect from the user list back to the department list, I have to do something like:

return RedirectToAction("Index", "Departments", new { companyId = model.CompanyId });

As things get more nested, I have to start passing more and more parameters so that the controller ac开发者_开发技巧tions have the context that they need.

Is there a better way to structure the routes for controllers that have "nested" functionality like this? Should they look more like: Companies/{id}/Departments/{id}/Users ?

Is there a good article that someone could link me to that can provide some advice for best practices when it comes to this?


You just need to add more routes on your Global.aspx which maps Urls to actions with more specific routes at the top. You can have something like:

routes.MapRoute("user","Users/Index/{companyId}{departmentId}", new {controller="",action=""}) routes.MapRoute("department","Departments/Index/{companyId}",new { controller="",action=""}) routes.MapRoute("company","Companies/Index",new { controller="",action=""})

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号