如何做高级ASP.NET MVC(3)路由(计算动作等)?
更新
考虑以下 8 个路由,其中 Administration
为 area,controller 为 EmployeesController
且Id 是 EmployeeId
:
Administration/Corporate/{controller}/{Id}/Phones/{PhoneId}/Delete
- 操作 =
删除手机
- 操作 =
管理/公司/{controller}/{Id}/Phones/{PhoneId}/Deactivate
- 操作 =
停用手机
- 操作 =
管理/公司/{controller}/{Id}/Phones/{PhoneId}/Activate
- 操作 =
激活手机
- 操作 =
管理/公司/{controller}/{Id}/Notes/{NoteId}/Delete
- 操作 =
删除笔记
- 操作 =
管理/公司/{controller}/{Id}/Files/{FileId}/Delete
- 操作 =
删除文件
- 操作 =
管理/公司/{controller}/{Id}/Addresses/{AddressId}/Delete
- 操作 =
删除地址
- 操作 =
管理/公司/{controller}/{Id}/Addresses/{AddressId}/Deactivate
- 操作 =
DeactivateAddress
- 操作 =
管理/公司/{controller}/{Id}/Addresses/{AddressId}/Activate
- 操作 =
ActivateAddress
- 操作 =
如何将其转换为:
Administration/Corporate/{controller}/{Id}/{object}/{ObjectId}/{action}
其中对象是Phones|Notes|Files|Addresses|?
,action是Delete|Deactivate|Activate|?
>?
- 我需要获取对象并将其单一化(我已经有代码)。
- object =
电话
(电话
)
- object =
- 采取操作并将其转换(重写?)为操作 + 对象(单数)。
- 操作 =
删除
+电话
(删除电话
)
- 操作 =
我可以做第二个#2之前的所有事情,问题是我怎样才能将操作转换为路由定义中的其他内容?
这一切有可能吗?如果能将我现在拥有的 8 条路由变成 1 条,那就太好了。这个例子只使用了我的 EmployeesController
,我的 CustomersController
是这个大小的两倍,所以大约有 16 条路由可以变成1。这样可以节省大量空间和代码。
无论如何,如果可能的话,我期待着建议和想法。
UPDATED
Considering the following 8 routes where Administration
is area, controller is EmployeesController
and Id is EmployeeId
:
Administration/Corporate/{controller}/{Id}/Phones/{PhoneId}/Delete
- action =
DeletePhone
- action =
Administration/Corporate/{controller}/{Id}/Phones/{PhoneId}/Deactivate
- action =
DeactivatePhone
- action =
Administration/Corporate/{controller}/{Id}/Phones/{PhoneId}/Activate
- action =
ActivatePhone
- action =
Administration/Corporate/{controller}/{Id}/Notes/{NoteId}/Delete
- action =
DeleteNote
- action =
Administration/Corporate/{controller}/{Id}/Files/{FileId}/Delete
- action =
DeleteFile
- action =
Administration/Corporate/{controller}/{Id}/Addresses/{AddressId}/Delete
- action =
DeleteAddress
- action =
Administration/Corporate/{controller}/{Id}/Addresses/{AddressId}/Deactivate
- action =
DeactivateAddress
- action =
Administration/Corporate/{controller}/{Id}/Addresses/{AddressId}/Activate
- action =
ActivateAddress
- action =
How can I transform it into:
Administration/Corporate/{controller}/{Id}/{object}/{ObjectId}/{action}
where object is Phones|Notes|Files|Addresses|?
and action is Delete|Deactivate|Activate|?
?
- I need to take object and singularize it (for which I already have the code).
- object =
Phones
(Phone
)
- object =
- Take action and transform (rewrite?) it into action + object (singularized).
- action =
Delete
+Phone
(DeletePhone
)
- action =
I can do everything up to the second #2 where the issue is how can I transform the action into something else in the route definition?
Is any of this even possible at all? It would be nice to take the 8 routes I have now and turn them into 1. And this example only uses my EmployeesController
, my CustomersController
is twice that size so about 16 routes that can be turned into 1. It would save a lot of space and code.
Anyway, I'm looking forward to suggestions and ideas if this is possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建自定义路由处理程序。
ASP.NET MVC 子域路由
You can create custom route handler.
ASP.NET MVC Subdomain Routing
我会设置一个简单的数组和方法来处理这种事情。它并不完全优雅,但应该很容易理解:
I would setup a simple array and method to handle this kind of thing. Its not exactly elegant, but it should be easy to understand: