カスタム バインダーを作成しましたが、実行できないようです。これが私のコードです:
public abstract class SlideItem
{
public string PlaceHolderName { get; set; }
public uint PlaceHolderID { get; set; }
public uint ItemType { get; set; }
}
public class TitleSlideItem : SlideItem
{
...
public TitleSlideItem()
{
ItemType = 1;
}
}
public class ParagraphSlideItem : SlideItem
{
...
public ParagraphSlideItem()
{
ItemType = 2;
}
}
public class SlideItemBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
System.Diagnostics.Trace.WriteLine("BindModel is working");
var values = (ValueProviderCollection)bindingContext.ValueProvider;
var placeHolderName = (string)values.GetValue("PlaceHolderName").ConvertTo(typeof(string));
var placeHolderID = (uint)values.GetValue("PlaceHolderID").ConvertTo(typeof(uint));
var itemType = (uint)values.GetValue("ItemType").ConvertTo(typeof(uint));
switch (itemType)
{
case 1:
System.Diagnostics.Trace.WriteLine("TitleSlideItemBinder");
return (SlideItem)new TitleSlideItem { PlaceHolderName = placeHolderName };
case 2:
System.Diagnostics.Trace.WriteLine("ParagraphSlideItem");
return (SlideItem)new ParagraphSlideItem { PlaceHolderName = placeHolderName };
case 3:
System.Diagnostics.Trace.WriteLine("TextSlideItem");
return (SlideItem)new TextSlideItem { PlaceHolderName = placeHolderName };
case 4:
System.Diagnostics.Trace.WriteLine("PictureSlideItem");
return (SlideItem)new PictureSlideItem { PlaceHolderName = placeHolderName };
default:
System.Diagnostics.Trace.WriteLine("this should never-ever-ever-ever happen");
//this should never-ever-ever-ever happen
return (SlideItem)new TextSlideItem { PlaceHolderName = placeHolderName };
}
}
}
これをglobal.asax.csに追加しました:
ModelBinders.Binders.Add(new KeyValuePair<Type, IModelBinder>(typeof(SlideItem), new SlideItemBinder()));
しかし、これを実行しようとすると:
[System.Web.Http.HttpPost]
public HttpResponseMessage Create(IList<SlideContent> l)
{
... }
私はまだ System.MissingMethodException というメッセージを受け取ります:抽象クラスを作成できません
どこが間違っているのですか?
心から、
ゾリ