繁体   English   中英

如何将初始json传递给MVC控制器中的另一个函数

[英]How to pass initial json to another function in MVC controller

我正在尝试使用MVC Controller实现类似状态机的功能。 客户端将json消息发送到控制器的动作Play。 每个消息都有一个MessageCode和一些其他数据,具体取决于MessageCode。

范例:

{MessageCode:1, words:["aaa","bbb","ccc"]}

{MessageCode:2, ClientsAge: 56, ClientsName:"Jon"}

...

因此,我有

public JsonResult Play(int MessageCode)
{
        switch(MessageCode){
         case 1:
//Perform some additional checks
         return _DoSomething1(words);
         case 2:
//Perform some additional checks
         return _DoSomething2(ClientsAge,ClientsName);
//
}

每个私有方法_DoSomething1,_DoSomething2等都有不同的签名。

您可以使用Json函数返回属性的JSON序列化,例如

return Json(ClientsName) 

并使用Json()转换为JSON

如果要序列化和反序列化JSON,请点击此链接如何:序列化和反序列化JSON数据

您可以创建一个自定义模型活页夹,该活页夹将您的Json输入转换成不同的模型(例如Step01ModelStep02Model ,...)

这样的问题中包含一些代码,应该为您指明正确的方向。

基本上,不是在一个动作方法中进行切换,而是在Model Binder中执行该操作,然后在类中调用您的重载动作:

public ActionResult Play(Step01Model step01Model) { ... }

public ActionResult Play(Step02Model step02Model) { ... }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM