问题标签 [controller-factory]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
authentication - ASP.NET MVC 创建需要身份验证的控制器,尽管用户未经身份验证,这会导致依赖注入问题
我在使用 ASP.NET 和 Unity 时遇到了问题。今天,需要经过身份验证的用户的控制器的构造函数有一些参数,只有在调用用户经过身份验证时才能由 Unity 填充。如果我调用导致此类控制器未验证的 URL,则 ASP.NET MVC 管道会尝试创建控制器,并且 Unit 会抛出异常,因为容器中不存在所需的对象。对于身份验证,我使用 WSFederationAuthenticationModule 和 SessionAuthenticationModule,在 web.config 中,我将“Forms”配置为身份验证模式,以强制 ASP.NET 重定向到登录页面。如果用户未经身份验证但直接重定向到登录页面,我预计管道不会创建控制器实例。
asp.net-mvc - 在 ASP.NET MVC 中创建自定义控制器工厂
我正在看一本书中的一个例子,作者试图创建一个自定义控制器工厂,如下所示。(仅指 CreateController 方法)
对于默认情况,controllerName 默认情况的值被设置为现有的控制器(产品)。给出的原因是,这将导致 MVC 框架搜索与备用控制器关联的视图,而不是用户请求的控制器。我的问题是为什么没有以类似的方式搜索案例产品和客户的视图?
c# - Implementing Custom Controller Factory involving multiple Assemblies
Currently, we are trying to implement a custom Controller Factory in our API to determine the correct controller to be used depending on an identifier token that is passed in to our API. The way this project is setup is that each different company (there are only about 5) has its own assembly which contains custom Controllers that has the Action Methods needed to execute whatever action someone is attempting to do. One of the requirements I've been sent is that the controllers must be named the same in these assemblies. So you could have, for example, four different Controllers each in a different assembly all named CustomerController
. These four controllers all contain Action Methods named the same as well but the implementation within them is completely different.
When using our custom Controller Factory, we're using Reflection to pull and create an instance of the correct controller type based off of which Assembly should be targeted. I've debugged this and confirmed that the correct Controller is being returned as a result.
The issue occurring happens during the Action Method call. Even though our Controller Factory is returning the correct controller for our request, it seems that MVC has possibly pre-loaded every controller from these assemblies and is still looking at these as a possible direction. The exact error we're getting on the Action Method getting called is: The current request is ambiguous between the following action methods
Is there any way to tell MVC to ignore the other controllers that were not returned in the request of the controller factory so it will know not to attempt to look at the other controllers that do not seem to be loaded? What is the point of a ControllerFactory if MVC is just going to check all the other controllers for an Action Method match? There must be something I'm missing here.
The only other solution I've found to work around this is adding a ActionMethodSelector attribute to all of the Action Methods involved. However, this makes the solution pretty brittle when looking forward and considering that every single Action Method would need to have the same attribute repeated across them.
Looking forward to any advice or knowledge on why MVC is behaving this way when I have implemented a custom Controller Factory that should be deciding which Controller is chosen for using an Action Method. Thanks in advance!
asp.net-mvc - 在 ASP.NET MVC 5 中使用 ControllerFactory 的控制器构造函数中的依赖注入
我正在开发 ASP.NET MVC 5 应用程序。我需要在控制器的构造函数中使用参数。DefaultControllerFactory 无法解决它,我从它继承了我自己的 ControllerFactory:
我在 Global.asax 中注册了它:
ControllerBuilder.Current.SetControllerFactory(new ControllerFactoryProvider());
但是当我运行我的应用程序时,无论使用 DefaultControllerFactory 都没有看到带参数的构造函数。
我在哪里可以出错?
model-view-controller - ObjectFactory 到底是什么,它的用途是什么?
这是我的StructureMapControllerFactory
,我想在 mvc5 项目中使用它
我像这样配置了我的控制器工厂global.asax
:
但什么是 ObjectFactory
?为什么我找不到任何关于它的命名空间?为什么iv得到:
当前上下文中不存在名称 ObjectFactory
我尝试了许多使用控制器工厂的方法,当我感觉到代码中的对象工厂时,我遇到了这个问题......这真的让我很无聊
c# - 我的控制器工厂似乎没有识别控制器
我想用 Autofac 创建一个控制器工厂,但在这一行中出现空引用实例错误:string ctrName = contr.ControllerContext.RouteData.Values["Controller"].ToString()
当我运行我的项目时。
这是我的controller factory
:
这是我的appstart:
错误原因很清楚:ControllerContext
我的 foreach 项目为空……但为什么呢?我的解决方案中有控制器:
更新:我在 foreach 上检查了调试器。项目类型是WebApplication1.Controllers.HomeController
指我的家庭控制器,但 mt 项目的控制器上下文为空!
c# - 如何在运行时创建新控制器?
想象一个MVC
在该解决方案中没有任何控制器的项目。我有同样的事情,想在运行时使用 aDefaultControllerFactory
然后在我Global.asax
的 by中注册它来创建控制器Autofac
。在这里,我有一个具有CreateController
方法的示例控制器工厂:
正如您在上面看到的,这一行:List<IController> lstControllers = DependencyResolver.Current.GetServices<IController>().ToList();
将获取使用IController
服务的控制器列表。但就我而言,解决方案中不存在控制器。我想在运行时创建一个新控制器并将其添加到我的_controllerMap
.
所以我将代码更改为:
那么contr
在指定行呢?此参数的类型必须为Func<RequestContext,IController>
。如何通过 C# 代码在功能上创建它?
asp.net-core-mvc - 修改asp.net core mvc中DefaultControllerFactory的CreateController方法中的ControllerContext
- 我想覆盖我的控制器方法。在这里,如果对CatalogController提出请求,我已经覆盖了DefaultControllerFactory的CreateController方法以返回CatalogCustomController对象。
但问题在于我需要将所有依赖项传递给控制器构造函数。
虽然我想做这样的事情,但通过修改 ControllerContext 上下文
c# - ASP.NET Core 3 - 覆盖自定义的默认 ControllerFactory,在其他情况下如何保存调用?
如果 DefaultControllerFactory 已成为 ASP.NET Core 3 中的内部类,如何使用自定义工厂覆盖默认实现 IControllerFactory 并在正常情况下继续调用 DefaultControllerFactory?
.NET 5 中的 DefaultControllerFactory 类是内部的,我不能调用它的 CreateController 方法来尝试在 ASP MVC Core 5 环境中注册常规控制器。