情况很复杂。FindAction 在 ControllerActionInvoker 上调用。这最终会调用 ReflectedControllerDescriptor.FindAction,后者又会调用 ActionMethodSelector.FindActionMethod,后者会调用同一类型的 RunSelectionFilters。该方法采用项圈传递给他的方法列表,并对其进行迭代,检查每个方法的参数并将它们与请求中的值进行比较。因为它必须在请求进来时快速运行,所以它们都被缓存了,而且因为它被设计为可扩展的,所以在我描述的层之间有一些抽象类型。因此,起初可能有点难以理解,并且可能难以将其重新用于非控制器逻辑。但是,您可以将其用作实现您自己的系统的模型。我认为它' 对于特定领域的应用程序来说有点太复杂了。扩展点的数量可能适合 MVC 框架,但适合您自己的代码 YAGNI。
但是,我希望这足以让您入门。
关于在没有 Web 堆栈的情况下使用模型绑定器:嗯,您仍然需要 MVC,但不一定需要 Web 服务器。以下是我们在单元测试中的做法:
internal static T Bind<T>(string prefix, FormCollection collection, ModelStateDictionary modelState) where T:BaseTimeRecordPresentationModel
{
var mbc = new ModelBindingContext()
{
ModelName = prefix,
ModelState = modelState,
ModelType = typeof(T),
ValueProvider = collection.ToValueProvider()
};
IModelBinder binder = new TimeRecordModelBinder();
var cc = new ControllerContext();
return binder.BindModel(cc, mbc) as T;
}
internal static T BindAndAssertValid<T>(string prefix, FormCollection collection) where T:BaseTimeRecordPresentationModel
{
var msd = new ModelStateDictionary();
var result = Bind<T>(prefix, collection, msd);
if (!msd.IsValid)
{
Assert.Fail(ModelStateValidationSummary(msd));
}
return result;
}