如何impelment自定义控制器工厂ASP.Net MVC [英] How to impelment a custom controller factory ASP.Net MVC
问题描述
我在MVC领域的新手。所以我还是学习并努力学习地段MVC事先区。所以我正在读一本关于自定义控制器工厂ASP.Net MVC职务。
i am newbie in mvc area. so i am learning still and trying to learn lots of advance area in MVC. so i was reading a post about custom controller factory ASP.Net MVC.
一个人解释那么少,所以我只是不明白如何实现。这里是帖子的网址
的在asp.net mvc的是有可能使通用控制器?
a guy explain so less so i just do not understand how to implement. here is the post url In asp.net mvc is it possible to make a generic controller?
您想 /产品/指数
引发 myController的<产品>的.index()
这可以通过编写自己的IControllerFactory和实施这样的 CreateController
方法来实现:
This can be accomplished by writing your own IControllerFactory and implementing the CreateController
method like this:
public IController CreateController(RequestContext requestContext, string controllerName)
{
Type controllerType = Type.GetType("MyController").MakeGenericType(Type.GetType(controllerName));
return Activator.CreateInstance(controllerType) as IController;
}
需要多一点的示例代码。只是不知道在哪里定义 CreateController
功能。我需要在基地控制器写入这个功能呢?
need a bit more sample code. just do not understand where to define CreateController
function. do i need to write this function in base controller ?
在收到请求时,比如 /产品/指数
或 /客户/指数
那么如何索引方法可以调用的基本控制器?
when request comes like /Product/Index
or /Customer/Index
then how index method can be invoke in base controller?
所以找指导像我这样的新手在MVC提前区域。谢谢
so looking for guidance for a newbie like me in MVC advance area. thanks
推荐答案
您需要实现的IControllerFactory (或从的 DefaultControllerFactory 其中包含一些额外的功能)。在此之后,使用的的Application_Start 控制器工厂=http://msdn.microsoft.com/en-us/library/dd460275%28v=vs 0.108%29.aspx> SetControllerFactory 方式:
You'll need to implement IControllerFactory (or inherit from DefaultControllerFactory which contains some extra functionality). After that, set your controller factory in Application_Start
by using SetControllerFactory method:
ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
希望这澄清了事情有点给你。
Hope this clarifies things a bit for you.
这篇关于如何impelment自定义控制器工厂ASP.Net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!