C#返回JSON而不是视图(MVC) [英] C# Return JSON instead of a view (MVC)

查看:92
本文介绍了C#返回JSON而不是视图(MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AJAX从控制器接收 JSON数组.在 php 中,您可以返回 string 而不是 view ,这样您可以执行以下操作:

I want to use AJAX to recieve a JSON array from the controller. In php you can just return string instead of a view so you can do something like that:

$array = array(
"Hello" => "hi", 
"Whatsup" => sup
);
return json_encode($array);

但是,在 C#中,您必须返回 ActionResult 对象.因此,直到现在,我还没有找到一个很好的解决方案来打印" JSON数组作为结果.

However, in C# you have to return ActionResult object. So until now I didn't find a good solution for "printing" the JSON array as the result.

推荐答案

您可以返回JsonResult而不是ActionResult来返回json:

You can return JsonResult instead of ActionResult to return your json:

  public JsonResult MyAction()
   {
    //DoSomething
    return Json(value);
   }

这篇关于C#返回JSON而不是视图(MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆