每条路线的控制器工厂
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I have implemented a custom route (inherit from RouteBase) to have dynamic routes based on the data stored in the database. After processing, this route ends up returning RouteData containing 1) An EF entity 2) action 3) controller

All works as expected except that I would like for the controller value to be either the full type name of a controller (I allow my users to select it from the admin panel) or the word 'Auto'. If Auto is selected I use structure map to locate a controller that implements a generic Controller<TEntityType>. To do this I return with the route data an MvcRouteHandler with a custom controller factory passed into it's constructor.

After a little digging I realized that the MvcRouteHandler does not pass that controller factory to the MvcHandler that it creates therefore my custom controller factory is never called and the route always fails. I am not sure what alternatives I have if any. I think I could probably set the controller factory in general but I feel that that would be wrong as only the requests handled by my custom routes should have the custom controller factory.

Thanks in advance, John

#0

In the end the following works. The only gray point is the ReleaseController method to which the framework does not pass a RequestContext. This is ok though because all that method does is call dispose on the controller if it implements IDisposible so the default implementation is fine.

   public class RouteControllerFactory : IControllerFactory
{
    private readonly DefaultControllerFactory Default = new DefaultControllerFactory();

    public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
    {
        return (requestContext.RouteData.Values.TryGetValue("controllerfactory") as IControllerFactory ?? Default).CreateController(requestContext, controllerName);
    }

    public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
    {
        return (requestContext.RouteData.Values.TryGetValue("controllerfactory") as IControllerFactory ?? Default).GetControllerSessionBehavior(requestContext, controllerName);
    }

    public void ReleaseController(IController controller)
    {
        Default.ReleaseController(controller);
    }
}

To register the controller factory just use ControllerBuilder.Current.SetControllerFactory(typeof(RouteControllerFactory));

at Application_Start

推荐文章

联合mySQL查询和限制

联合mySQL查询和限制

推荐文章

从url解码图像时内存不足

从url解码图像时内存不足

推荐文章

iOS持久应用程序

iOS持久应用程序

推荐文章

试着学习课堂和“堆”c++

试着学习课堂和“堆”c++

推荐文章

MVP设计困境

MVP设计困境

推荐文章

在控制台中从3D数组中绘制时间表

在控制台中从3D数组中绘制时间表

推荐文章

550 ftp期间出现文件名无效错误

550 ftp期间出现文件名无效错误

推荐文章

自定义修饰符在静态分析中的Pydiction支持

自定义修饰符在静态分析中的Pydiction支持

推荐文章

在Ubuntu11.10上配置android时出错

在Ubuntu11.10上配置android时出错

推荐文章

与django taggit最常见

与django taggit最常见

推荐文章

从调用的方法启动生产者任务?

从调用的方法启动生产者任务?

推荐文章

将IR操作数与LLVM中的常量进行比较

将IR操作数与LLVM中的常量进行比较

推荐文章

如何解决IE8中的XSS问题?

如何解决IE8中的XSS问题?

推荐文章

SQL CE 4.0和ASP.NETMVC 3型

SQL CE 4.0和ASP.NETMVC 3型

推荐文章

在Inno Setup isl文件中设置文本格式的选项

在Inno Setup isl文件中设置文本格式的选项

推荐文章

从xml到php的编码?

从xml到php的编码?