gpt4 book ai didi

c# - 实现自定义 Controller 工厂 ASP.NET MVC

转载 作者:行者123 更新时间:2023-11-30 19:10:06 25 4
gpt4 key购买 nike

<分区>

我正在阅读一篇关于自定义 Controller 工厂 ASP.NET MVC 的文章。

有人解释了,但我就是不明白如何实现。这是帖子网址 In asp.net mvc is it possible to make a generic controller?

他们说

You would like /Product/Index to trigger MyController<Product>.Index()

这可以通过编写您自己的 IControllerFactory 并实现 CreateController 来实现像这样的方法:

public IController CreateController(RequestContext requestContext, string controllerName)
{
Type controllerType = Type.GetType("MyController").MakeGenericType(Type.GetType(controllerName));
return Activator.CreateInstance(controllerType) as IController;
}

需要更多示例代码。只是不明白在哪里定义CreateController功能。我需要在基本 Controller 中编写这个函数吗?

当请求出现时 /Product/Index/Customer/Index那么如何在基础 Controller 中调用索引方法呢?

所以在 MVC 高级领域寻找像我这样的新手的指导。谢谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com