在Asp MVC中的URL结构是这样
In Asp MVC the url structure goes like
mysite/ {控制器} / {行动} / {ID}
mysite/{controller}/{action}/{id}
对于每一个控制器,说 mysite/blog ,有一个BlogController。
For each "controller", say mysite/blog, there is a BlogController.
但是URL我的{控制器}部分没有决定pre-手,但它在运行时动态确定,如何创建一个动态控制器映射任何东西到同一个控制器,然后根据在值,并确定做什么?
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 senario?
推荐答案当然!你需要重写 DefaultControllerFactory 来找到一个自定义的控制,如果一个不存在。然后,你需要写一个 IActionInvoker 来处理动态操作名称。
Absolutely! You'll need to override the DefaultControllerFactory to find a custom controller if one doesn't exist. Then you'll need to write an IActionInvoker to handle dynamic action names.
您控制器工厂看起来是这样的:
Your controller factory will look something like:
public class DynamicControllerFactory : DefaultControllerFactory { private readonly IServiceLocator _Locator; public DynamicControllerFactory(IServiceLocator locator) { _Locator = locator; } protected override Type GetControllerType(string controllerName) { var controllerType = base.GetControllerType(controllerName); // if a controller wasn't found with a matching name, return our dynamic controller return controllerType ?? typeof (DynamicController); } protected override IController GetControllerInstance(Type controllerType) { var controller = base.GetControllerInstance(controllerType) as Controller; var actionInvoker = _Locator.GetInstance<IActionInvoker>(); if (actionInvoker != null) { controller.ActionInvoker = actionInvoker; } return controller; } }那么你的行动调用会是这样:
Then your action invoker would be like:
public class DynamicActionInvoker : ControllerActionInvoker { private readonly IServiceLocator _Locator; public DynamicActionInvoker(IServiceLocator locator) { _Locator = locator; } protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName) { // try to match an existing action name first var action = base.FindAction(controllerContext, controllerDescriptor, actionName); if (action != null) { return action; } // @ray247 The remainder of this you'd probably write on your own... var actionFinders = _Locator.GetAllInstances<IFindAction>(); if (actionFinders == null) { return null; } return actionFinders .Select(f => f.FindAction(controllerContext, controllerDescriptor, actionName)) .Where(d => d != null) .FirstOrDefault(); } }您可以看到这个code 的多很多。这是由我的老尝试初稿,并在写一个完全动态的MVC管道同事。你可以自由使用它作为参考,并复制你想要的东西。
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.
修改
我想我应该包括有关code所做的一些背景知识。我们试图动态围绕一个领域模型的MVC层。所以,如果您的域名包含在产品类,你可以浏览到产品\\承滴盘来查看所有产品的列表。如果你想添加一个产品,你会浏览到产品\\添加。你可以去产品\\编辑\\ 1 编辑产品。我们甚至尝试过的事情一样,让您在实体编辑属性。因此,产品\\ editprice \\ 1?值= 42 将产品#1的价格属性设置为42(我的路径可能有点过,我不记得了确切的语法了。)希望这有助于!
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 to product\add. You could go to product\edit\1 to edit a product. We even tried things like allowing you to edit properties on an entity. So product\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!
更多推荐
如何实现在ASP.NET MVC动态控制器和操作方法?
发布评论