在 ASP MVC 2 中向控制器发送参数
我正在编写一个用于管理多个子站点的区域,几乎每个站点的所有功能都是相同的(添加/编辑/删除页面等),并且我的实例化存储库采用 SiteIdentity,因此所有数据访问方法都与这。我现在面临的问题是试图让我的行动方法变得不可知。
我想要使用的 URL 模式类似于:
"ExternalSite/{identity}/{controller}/{action}/{id}"
一种简单的方法是让每个操作都采用身份参数,但这意味着必须在每个操作中将其传递到我的存储库,并将其包含在几个 UI 元素的 ViewData 中。我宁愿让这种事情在控制器中发生一次,例如在其构造函数中。
最好的方法是什么?目前我能想到的最好办法是尝试从 RouteData 字典中查找并转换身份,但我觉得应该有一个更优雅的解决方案。
I am writing an area for administering several subsites, almost all of the functionality will be the same across each site (add/edit/remove pages etc) and my repository on instantiation takes the SiteIdentity so all the data access methods are agnostic in relation to this. The problem I have at the moment is trying to make my action methods also agnostic.
The URL pattern I want to use is along the lines of:
"ExternalSite/{identity}/{controller}/{action}/{id}"
A naive approach is to have each action take the identity parameter, but this means having to pass this in to my repository on each action as well as include it in the ViewData for a couple of UI elements. I'd much rather have this something that happens once in the controller, such as in its constructor.
What is the best way to do this? Currently the best I can come up with is trying to find and cast identity from the RouteData dictionary but part of me feels like there should be a more elegant solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您想在每次拥有特定参数名称(也称为 RouteData 字典键)时使用 OnActionExecuting 或自定义 ModelBinder 来执行该逻辑。
It sounds like you want to use OnActionExecuting or a Custom ModelBinder to do that logic each time you have a specific parameter name (also known as a RouteData dictionary key).
您可以访问
Request.RequestContext.RouteData
中的路由值,因此您可以创建基本控制器和公共属性 SiteIdentity,在这种情况下,您可以从所有继承的控制器中的所有操作访问它。You have access to your route values in
Request.RequestContext.RouteData
, so you can make base controller and public property SiteIdentity, in such case you can access it from all actions in all inherited controllers.