"无法创建接口实例"(Orchard CMS中的MvC 3向导)

M E*_*rty 2 orchardcms asp.net-mvc-3

基于asp.net mvc(拆分视图模型,单一模型)多步注册过程问题的优秀答案,我使用了Darin Dimitrov提供的示例来测试ASP.net MVC3向导.它独立运行,但不在Orchard CMS v1.3中.

我收到以下错误:


'/'应用程序中的服务器错误.

无法创建接口的实例.

描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.MissingMethodException:无法创建接口的实例.

来源错误:

在执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息.

堆栈跟踪:

[MissingMethodException:无法创建接口的实例.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,Boolean publicOnly,Boolean noCheck,Boolean&canBeCached,RuntimeMethodHandleInternal&ctor,Boolean&bNeedSecurityCheck)+0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean skipCheckThis, Boolean fillCache)+98
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,Boolean skipVisibilityChecks,Boolean skipCheckThis,Boolean fillCache)+241 System.Activator.CreateInstance(Type type,Boolean nonPublic)+69
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Type modelType)+199 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+572
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+449
System.Web. Mvc.ControllerActionInvoker.GetPa rameterValue(ControllerContext controllerContext,ParameterDescriptor parameterDescriptor)+317
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext,ActionDescriptor actionDescriptor)+117
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext,String actionName)+343
System.Web. Mvc.Controller.ExecuteCore()+116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)+97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)+10
System.Web .Mvc.<> c__DisplayClassb.b__5()+37
System.Web.Mvc.Async.<> c__DisplayClass1.b__0()+21
System.Web.Mvc.Async.<> c__DisplayClass8 1.<BeginSynchronous>b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult1.End()+ 62 System.Web .Mvc.<> c__DisplayClasse.b__d()+50
System.Web.Mvc.SecurityUtil.b__0(Action f)+7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)+22 System.Web.Mvc.MvcHandler. EndProcessRequest(IAsyncResult asyncResult)+60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandl er.EndProcessRequest(IAsyncResult result)+9
Orchard.Mvc.Routes.HttpAsyncHandler.EndProcessRequest(IAsyncResult result)在d:\ Builds\OrchardFull\src\Orchard\Mvc\Routes\ShellRoute.cs:148
System.Web.CallHandlerExecutionStep.System .Web.HttpApplication.IExecutionStep.Execute()+8963149 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&completedSynchronously)+184

版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.237


我冒险猜测这是控制器引起了大惊小怪.这是代码:

[Themed]
    public class WizardController : Controller
    {
        public ActionResult Index()
        {
            var wizard = new WizardViewModel();
            wizard.Initialize();
            return View(wizard);
        }

        [HttpPost]
        public ActionResult Index([Deserialize] WizardViewModel wizard, IStepViewModel step)
        {
            wizard.Steps[wizard.CurrentStepIndex] = step;
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(Request["next"]))
                {
                    wizard.CurrentStepIndex++;
                }
                else if (!string.IsNullOrEmpty(Request["prev"]))
                {
                    wizard.CurrentStepIndex--;
                }
                else
                {
                    // TODO: we have finished: all the step partial
                    // view models have passed validation => map them
                    // back to the domain model and do some processing with
                    // the results

                    return Content("thanks for filling this form", "text/plain");
                }
            }
            else if (!string.IsNullOrEmpty(Request["prev"]))
            {
                // Even if validation failed we allow the user to
                // navigate to previous steps
                wizard.CurrentStepIndex--;
            }
            return View(wizard);
        }
    }
Run Code Online (Sandbox Code Playgroud)

但我不是百分百肯定,另一个罪魁祸首可能是WizardViewModel:

[Serializable]
public class WizardViewModel
{
    public int CurrentStepIndex { get; set; }
    public IList<IStepViewModel> Steps { get; set; }

    public void Initialize()
    {
        Steps = new IStepViewModel[]
        {
            new Step1ViewModel(),
            new Step2ViewModel(),
            new Step3ViewModel()
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

最后一部分是手动插入步骤并更改最初发布的代码(直接来自作者).任何帮助是极大的赞赏.

Eil*_*aee 5

显然,您使用的Model Binder无法找到从提供的接口(IStepViewModel)实例化所需的模型

我很确定默认的ASP.NET MVC ModelBinder不能做这样的事情,因为它需要一些自定义逻辑来选择应该实例化的具体接口实现.

因此,您有两个解决方案:

  1. 检查你的样本,我认为它提供了一个自定义的ModelBinder,并通过实现一个IModelBinderProvider在Orchard中注册它
  2. 不要在Index操作中要求IStepViewModel:

    public ActionResult Index([Deserialize] WizardViewModel wizard, IStepViewModel step)
    
    Run Code Online (Sandbox Code Playgroud)

    并用具体的类替换它.