2010-04-29 260 views
2

您好我正在使用與asp.net mvc 2.0自定義模型綁定器,一切工作本地,但部署到服務器運行iis 7時,我得到一個奇怪的錯誤,很難得到有關信息錯誤如下,我還附上我的模型綁定類asp.net mvc自定義模型綁定

錯誤:

[MissingMethodException: Method not found: 'System.Collections.Generic.IDictionary`2<System.String,System.Web.Mvc.ValueProviderResult> System.Web.Mvc.ModelBindingContext.get_ValueProvider()'.] 
FID.Binders.documentModelBinder.GetValue(ModelBindingContext bindingContext, String key) in C:\Users\Bich Vu\Documents\Visual Studio 2010\Projects\FID\FID\Binders\DocumentModelBinder.cs:155 
FID.Binders.documentModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) in C:\Users\Bich Vu\Documents\Visual Studio 2010\Projects\FID\FID\Binders\DocumentModelBinder.cs:61 
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +475 
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +152 
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +709 
System.Web.Mvc.Controller.ExecuteCore() +162 
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +58 
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20 
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371 Below is the code file 

代碼:

public class BinddocumentAttribute : DataBindAttribute 
{ 
    public BinddocumentAttribute() : base(typeof(documentModelBinder)) { } 

    public class documentModelBinder : DefaultModelBinder 
    { 
    ICategoryService _CategoryService= ServiceLocator.Current.GetInstance<ICategoryService>(); 
    IFilePersistor _persistor = ServiceLocator.Current.GetInstance<IFilePersistor>(); 
    IFileTypeService _FiletypeService = ServiceLocator.Current.GetInstance<IFileTypeService>();  

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var doc = base.BindModel(controllerContext, bindingContext) as Document; 

     string key = bindingContext.ModelName; 

     var errors = Core.xval.DataAnnotationsValidationRunner.GetErrors(doc); 

     try 
     { 
      if (errors.Any()) 
       throw new xVal.ServerSide.RulesException(errors); 

      foreach (string inputTagName in controllerContext.HttpContext.Request.Files) 
      { 
      HttpPostedFileBase filebase = controllerContext.HttpContext.Request.Files[inputTagName]; 
      if (filebase.ContentLength==0) 
       break; 
      string extension= Path.GetExtension(filebase.FileName); 

      if (!filebase.ContentType.Contains("image/")) 
      { 
       if (extension != _FiletypeService.GetFiletype(GetValue<short>(bindingContext, "Filetype_id")).extension.Trim()) 
       { 
       bindingContext.ModelState.AddModelError("filetype", "Verify that the file type matches the selected file type");       
       throw new RulesException("filetype", "Verify that the file type matches the selected file type", doc); 

       } 
      } 
      } 
     } 
     catch (xVal.ServerSide.RulesException ex) 
     { 
      return null; 
     } 

     doc.Category1 = _CategoryService.GetCategory(GetValue<int>(bindingContext, "cat.parent_id")); 
     doc.FileType1 = _FiletypeService.GetFiletype(GetValue<short>(bindingContext, "Filetype_id")); 
     doc.modifieddate = DateTime.Now; 

     if (doc.IsNewDocument) 
     { 
      doc.CreateParentFolders(); 
      doc.createdate = DateTime.Now; 
     } 

     UpdateFiles(controllerContext, doc); 
     return doc; 
    } 

    private void UpdateFiles(ControllerContext controllerContext, Document doc) 
    { 
     foreach (string inputTagName in controllerContext.HttpContext.Request.Files) 
     { 
     HttpPostedFileBase filebase = controllerContext.HttpContext.Request.Files[inputTagName]; 
     if (filebase.ContentLength > 0) 
     { 
      if (filebase.ContentType.Contains("image/")) 
      { 
      Thumb image = new Thumb { type = filebase.ContentType, name = filebase.FileName, PostedFile = filebase, AssociatedDocument = doc, document_id=doc.document_id }; 
      image.filepath = _persistor.PersistFile(image); 
      doc.Thumbs.Add(image); 
      } 
      else 
      { 
      doc.PostedFile = filebase; 
      doc.filesize = long.Parse(filebase.ContentLength.ToString()); 
      doc.filepath = _persistor.PersistFile(doc); 
      } 
     } 
     } 
    } 

    private T GetValue<T>(ModelBindingContext bindingContext, string key) 
    { 
     ValueProviderResult valueResult; 
     bindingContext.ValueProvider.TryGetValue(key, out valueResult); 
     bindingContext.ModelState.SetModelValue(key, valueResult); 
     return (T)valueResult.ConvertTo(typeof(T)); 
    } 

    protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var document = bindingContext.Model as Document; 
     if (String.IsNullOrEmpty(document.title)) 
     { 
      bindingContext.ModelState.AddModelError("Name", "..."); 
     } 
    } 
} 
+0

請問您的服務器已安裝了.NET Framework的正確版本更換? – R0MANARMY 2010-04-29 15:11:35

+0

可能在system.web.mvc版本的差異是原因,我想知道同樣的事情,我的本地版本是v2.0.50727而服務器版本是2.0.50217.0 – user327087 2010-04-29 15:20:43

+0

服務器正在運行.net框架4.0 – user327087 2010-04-29 15:21:09

回答

3

首先,你應該確保你運行正確的組件

二 在MVC2以下是無效的。該IValueProvider接口已經改變

bindingContext.ValueProvider.TryGetValue(key, out valueResult); 

valueResult = bindingContext.ValueProvider.GetValue(key); 
+0

多數民衆贊成是完美的答案,謝謝約翰。不同版本的asp.mvc是問題 – user327087 2010-04-29 15:46:46

+0

從MVC 1轉到MVC 2時,遇到類似的問題並非問題。 – 2010-04-29 16:03:56

+0

非常感謝。 – Kayes 2010-07-03 16:26:02

0

一個缺少方法例外,聽起來像在您的代碼編譯SUC在推送機上合理地使用,但是引用的DLL並沒有將它傳遞給生產服務器。我會檢查bin是否包含不同數量的DLL,每個DLL的版本是否在外,以及生產機器是否缺少來自GAC的某些DLL。

+0

可能system.web.mvc版本的差異是原因,我想知道同樣的事情,我的本地版本是v2.0.50727,而服務器版本是2.0.50217.0 – user327087 2010-04-29 15:20:17

+0

當然。始終驗證您用於構建的相同DLL版本與產品中的版本相同。 – Tejs 2010-04-29 15:24:03