C# 将接口实现作为json从客户端发送时出现问题
我有一个接口和两个实现它的类C# 将接口实现作为json从客户端发送时出现问题,c#,json,C#,Json,我有一个接口和两个实现它的类 namespace FirebaseNet.Messaging { public interface INotification { string Title { get; set; } } public class AndroidNotification : INotification { public string Title { get; set; } } public
namespace FirebaseNet.Messaging
{
public interface INotification
{
string Title { get; set; }
}
public class AndroidNotification : INotification
{
public string Title { get; set; }
}
public class IOSNotification : INotification
{
public string Title { get; set; }
}
}
现在我又上了一节这样的课
public class Message
{
public INotification Notification { get; set; }
}
消息参数像这样传递给类
[HttpPost]
public async Task<JsonResult> SendMessage(Message message)
或
到目前为止,一切正常。现在我想把AJAX发布到SendMessage
。我试着用这个DTO
JavaScript代码
var message = {
Notification : {
Title : "Portugal vs. Denmark"
}
};
这显然是失败的
无法创建接口的实例
什么是理想的解决方案
附言:考虑将消息
类更改为
public class Message
{
public INotification Notification { get; set; }
public AndroidNotification AndroidNotification { get; set; }
public IOSNotification IOSNotification { get; set; }
}
这是第三方DLL,我不想碰它 实现这一点的一种方法是编写自定义模型绑定器:
public class NotificationModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var typeValue = bindingContext.ValueProvider.GetValue("ModelType");
var type = Type.GetType(
(string)typeValue.ConvertTo(typeof(string)),
true
);
if (!typeof(INotification).IsAssignableFrom(type))
{
throw new InvalidOperationException("Bad Type");
}
var model = Activator.CreateInstance(type);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
return model;
}
}
引导应用程序时要注册的:
ModelBinders.Binders.Add(
typeof(INotification),
new NotificationModelBinder()
);
这将允许客户端指定通知的具体类型:
var message = {
ModelType: "WebApplication1.Models.AndroidNotification",
Notification : {
Title : "Portugal vs. Denmark"
}
};
或:
当然,您可以调整模型绑定器以获得指示混凝土类型和可能值的确切属性名称。在我的示例中,应该使用完全限定的类型名称,但您可以使用更友好的名称进行一些映射。实现这一点的一种方法是编写自定义模型绑定器:
public class NotificationModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var typeValue = bindingContext.ValueProvider.GetValue("ModelType");
var type = Type.GetType(
(string)typeValue.ConvertTo(typeof(string)),
true
);
if (!typeof(INotification).IsAssignableFrom(type))
{
throw new InvalidOperationException("Bad Type");
}
var model = Activator.CreateInstance(type);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
return model;
}
}
引导应用程序时要注册的:
ModelBinders.Binders.Add(
typeof(INotification),
new NotificationModelBinder()
);
这将允许客户端指定通知的具体类型:
var message = {
ModelType: "WebApplication1.Models.AndroidNotification",
Notification : {
Title : "Portugal vs. Denmark"
}
};
或:
当然,您可以调整模型绑定器以获得指示混凝土类型和可能值的确切属性名称。在我的示例中,应该使用完全限定的类型名称,但您可以使用更友好的名称进行一些映射。我有project
p1.FirebaseNet
,它在WebApp1
中引用。我在json调用中尝试了p1.FirebaseNet.Messaging.AndroidNotification
(以及FirebaseNet.Messaging.AndroidNotification
)作为ModelType
。它失败,出现异常无法从程序集“WebApp1,Version.”加载类型“p1.FirebaseNet.Messaging.AndroidNotification”。我可能做错了什么?我有一个项目p1.FirebaseNet
,它在WebApp1
中引用。我在json调用中尝试了p1.FirebaseNet.Messaging.AndroidNotification
(以及FirebaseNet.Messaging.AndroidNotification
)作为ModelType
。它失败,出现异常无法从程序集“WebApp1,Version..”加载类型“p1.FirebaseNet.Messaging.AndroidNotification”
我可能做错了什么?