且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在asp.net mvc的是有可能使通用控制器?

更新时间:2023-01-06 22:04:31

如果我理解你正确,你正在尝试做的,是借道类型T的通用控制器,一个给定的模型的所有请求。

If I understand you properly, what you are trying to do, is route all requests for a given Model through a generic controller of type T.

您会喜欢T的基础上要求模型有所不同。

You would like the T to vary based on the Model requested.

您想 /产品/指数引发 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;
	}