如何在ASP.NET MVC中实现动态控制器和动作方法?
在 Asp.net MVC 中,url 结构类似于
http://example.com/{controller}/{action}/ {id}
对于每个“控制器”,例如 http://example.com/blog,都有一个 BlogController。
但是我的 url 的 {controller} 部分不是预先决定的,而是在运行时动态确定的,如何创建一个“动态控制器”,将任何内容映射到同一控制器,然后根据该值并确定什么做什么?
与 {action} 相同,如果我的 url 的 {action} 部分也是动态的,有没有办法对这种情况进行编程?
In Asp.net MVC the url structure goes like
http://example.com/{controller}/{action}/{id}
For each "controller", say http://example.com/blog, there is a BlogController.
But my {controller} portion of the url is not decided pre-hand, but it is dynamically determined at run time, how do I create a "dynamic controller" that maps anything to the same controller which then based on the value and determines what to do?
Same thing with {action}, if the {action} portion of my url is also dynamic, is there a way to program this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
绝对地!如果自定义控制器不存在,您需要重写
DefaultControllerFactory
来查找自定义控制器。然后,您需要编写一个IActionInvoker
来处理动态操作名称。您的控制器工厂将类似于:
然后您的操作调用程序将类似于:
您可以看到更多此代码 此处。这是我和一位同事编写完全动态 MVC 管道的旧初稿尝试。您可以随意使用它作为参考并复制您想要的内容。
编辑
我想我应该包括一些关于该代码的作用的背景知识。我们试图围绕域模型动态构建 MVC 层。因此,如果您的域包含 Product 类,您可以导航到
products\alls
以查看所有产品的列表。如果您想添加产品,请导航至product\add
。您可以前往product\edit\1
编辑产品。我们甚至尝试过允许您编辑实体的属性之类的事情。因此product\editprice\1?value=42
会将产品 #1 的价格属性设置为 42。(我的路径可能有点偏离,我记不起确切的语法了。)希望这有帮助!Absolutely! You'll need to override the
DefaultControllerFactory
to find a custom controller if one doesn't exist. Then you'll need to write anIActionInvoker
to handle dynamic action names.Your controller factory will look something like:
Then your action invoker would be like:
You can see a lot more of this code here. It's an old first draft attempt by myself and a coworker at writing a fully dynamic MVC pipeline. You're free to use it as a reference and copy what you want.
Edit
I figured I should include some background about what that code does. We were trying to dynamically build the MVC layer around a domain model. So if your domain contained a Product class, you could navigate to
products\alls
to see a list of all products. If you wanted to add a product, you'd navigate toproduct\add
. You could go toproduct\edit\1
to edit a product. We even tried things like allowing you to edit properties on an entity. Soproduct\editprice\1?value=42
would set the price property of product #1 to 42. (My paths might be a little off, I can't recall the exact syntax anymore.) Hope this helps!经过更多思考后,您可能有比我的其他答案更简单的方法来处理动态操作名称。您仍然需要覆盖默认控制器工厂。我认为你可以像这样定义你的路线:
然后在你的默认/动态控制器上你会有
After a little more reflection, there may be a bit simpler way for you to handle the dynamic action names than my other answer. You'll still need to override the default controller factory. I think you could define your route like:
Then on your default/dynamic controller you'd have
您需要编写自己的
IControllerFactory
(或者可能从DefaultControllerFactory
派生),然后将其注册到ControllerBuilder
。You need to write your own
IControllerFactory
(or perhaps derive fromDefaultControllerFactory
) and then register it withControllerBuilder
.我正在 .Core 中使用它,但我将向所有人分享它的 MVC 版本,之后我将分享核心版本
// 一些代码
// 和
Iam working with it in .Core but i'll share it's MVC version for all, after that i will share the core version
// some codes
// and