返回JSON或部分HTML的ASP.NET MVC控制器操作

438

我正在尝试创建控制器操作,根据参数返回JSON或部分HTML。什么是将结果异步返回到MVC页面的最佳方法?

我应该如何将结果异步返回至MVC页面?
11个回答

550

在你的操作方法中,返回 Json(object) 以将 JSON 返回给你的页面。

public ActionResult SomeActionMethod() {
  return Json(new {foo="bar", baz="Blech"});
}

然后使用Ajax调用操作方法。您可以使用ViewPage中的任一帮助程序方法,例如

<%= Ajax.ActionLink("SomeActionMethod", new AjaxOptions {OnSuccess="somemethod"}) %>

SomeMethod 是一个 JavaScript 方法,然后对返回的 Json 对象进行评估。

如果想返回纯字符串,可以直接使用 ContentResult:

public ActionResult SomeActionMethod() {
    return Content("hello world!");
}

默认情况下,ContentResult会以text/plain作为其contentType返回。
这可以进行重载,因此你也可以这样做:

return Content("<xml>This is poorly formatted xml.</xml>", "text/xml");

10
抱歉Phil!这并没有回答问题,对吧?虽然肯定有帮助,但正如Brad所说,你需要想办法弄清楚他们在问什么,并相应地返回结果。 - Simon_Weaver
请看我在https://dev59.com/EnRB5IYBdhLWcg3w4bAo上的某些相关问题(好吧,这个问题引导我来到这里)。 - Simon_Weaver
9
如果你找到了答案,将其链接到问题本身。另外,我不认为将此选为答案是正确的做法。 - Cherian
这个链接是关于如何使用JSON和jQuery将复杂对象数组发送到ASP.NET MVC控制器的问题。 - Cherian
那个Json类的完全限定名称是什么? - Josh Withee
显示剩余2条评论

115

我认为你应该考虑请求的AcceptTypes。在我的当前项目中,我使用它来返回正确的内容类型,如下所示。

您在控制器上的操作可以通过请求对象进行测试。

if (Request.AcceptTypes.Contains("text/html")) {
   return View();
}
else if (Request.AcceptTypes.Contains("application/json"))
{
   return Json( new { id=1, value="new" } );
}
else if (Request.AcceptTypes.Contains("application/xml") || 
         Request.AcceptTypes.Contains("text/xml"))
{
   //
}

您可以实现视图的aspx以满足部分xhtml响应的情况。

然后在jQuery中,您可以传递type参数作为json来获取它:

$.get(url, null, function(data, textStatus) {
        console.log('got %o with status %s', data, textStatus);
        }, "json"); // or xml, html, script, json, jsonp or text

5
谢谢James,这样做可能非常有用,可以使用相同的控制器操作来创建类似于网站和REST API的东西。 - NathanD
如果我的控制器中有许多类似这样的方法,是否有更通用的方法可以实现? - Seph
Json类在哪个命名空间中?project.json的依赖是什么?提前致谢。 - Andrei
1
这是来自 System.Web.Mvc 的 JsonResult 类(在 System.Web.Mvc.dll 中)@Andrei。 - James Green
谢谢,找到了。也许更新答案以反映新的API?顺便说一下,我正在使用Microsoft.AspNetCore.Mvc.JsonResult的dotnet core。 - Andrei

84

另一个处理JSON数据的好方法是使用JQuery的getJSON函数。您可以调用

public ActionResult SomeActionMethod(int id) 
{ 
    return Json(new {foo="bar", baz="Blech"});
}

通过简单地使用jQuery的getJSON方法从方法...

$.getJSON("../SomeActionMethod", { id: someId },
    function(data) {
        alert(data.foo);
        alert(data.baz);
    }
);

17
这完全没有回答问题。 - Aaronaught
2
@Aaronaught 实际上第一部分 return Json(new {foo="bar", baz="Blech"}); 就是! - SparK
还要考虑使用 $.post。参见 https://dev59.com/dnRB5IYBdhLWcg3wAjNH (ASP.Net MVC默认禁用JSON Get请求,出于安全原因)。 - Greg

57
我在使用JQuery实现MVC ajax GET调用时遇到了一些问题,导致我头痛不已,因此在这里分享解决方案。
  1. 确保在ajax调用中包括数据类型“json”。这将自动为您解析返回的JSON对象(假设服务器返回有效的JSON)。
  2. 包含JsonRequestBehavior.AllowGet;没有这个,MVC会返回HTTP 500错误(在客户端指定dataType:json)。
  3. 在$.ajax调用中添加cache:false,否则您最终会收到HTTP 304响应(而不是HTTP 200响应),服务器将无法处理您的请求。
  4. 最后,json区分大小写,因此元素的大小写在服务器端和客户端必须匹配。

示例JQuery:

$.ajax({
  type: 'get',
  dataType: 'json',
  cache: false,
  url: '/MyController/MyMethod',
  data: { keyid: 1, newval: 10 },
  success: function (response, textStatus, jqXHR) {
    alert(parseInt(response.oldval) + ' changed to ' + newval);                                    
  },
  error: function(jqXHR, textStatus, errorThrown) {
    alert('Error - ' + errorThrown);
  }
});

样例MVC代码:

[HttpGet]
public ActionResult MyMethod(int keyid, int newval)
{
  var oldval = 0;

  using (var db = new MyContext())
  {
    var dbRecord = db.MyTable.Where(t => t.keyid == keyid).FirstOrDefault();

    if (dbRecord != null)
    {
      oldval = dbRecord.TheValue;
      dbRecord.TheValue = newval;
      db.SaveChanges();
    }
  }

    return Json(new { success = true, oldval = oldval},
                JsonRequestBehavior.AllowGet);
}

12

回答问题的另一半,您可以拨打以下电话:

return PartialView("viewname");

当你想返回部分HTML时,你只需要找到某种方法来决定请求是想要JSON还是HTML,也许可以根据URL的一部分或参数来确定。


3
那么问题是否仍然没有得到解答呢? - Simon_Weaver
2
这并没有回答问题。 - Aaronaught
他正在寻找一个Ajax请求来获取使用PartialView的HTML,除非您从一个使用Ajax调用的操作方法返回视图,否则需要刷新页面。 - Chris McGrath

7

使用incoding framework进行替代的解决方案

Action返回json格式数据

控制器(Controller)

    [HttpGet]
    public ActionResult SomeActionMethod()
    {
        return IncJson(new SomeVm(){Id = 1,Name ="Inc"});
    }

Razor页面

@using (var template = Html.Incoding().ScriptTemplate<SomeVm>("tmplId"))
{
    using (var each = template.ForEach())
    {
        <span> Id: @each.For(r=>r.Id) Name: @each.For(r=>r.Name)</span>
    }
}

@(Html.When(JqueryBind.InitIncoding)
  .Do()
  .AjaxGet(Url.Action("SomeActionMethod","SomeContoller"))
  .OnSuccess(dsl => dsl.Self().Core()
                              .Insert
                              .WithTemplate(Selector.Jquery.Id("tmplId"))
                              .Html())
  .AsHtmlAttributes()
  .ToDiv())

动作返回HTML

控制器

    [HttpGet]
    public ActionResult SomeActionMethod()
    {
        return IncView();
    }

Razor页面

@(Html.When(JqueryBind.InitIncoding)
  .Do()
  .AjaxGet(Url.Action("SomeActionMethod","SomeContoller"))
  .OnSuccess(dsl => dsl.Self().Core().Insert.Html())
  .AsHtmlAttributes()
  .ToDiv())

6

5

PartialViewResult 和 JSONReuslt 继承自基类 ActionResult。因此,如果返回类型是动态确定的,请将方法输出声明为 ActionResult。

public ActionResult DynamicReturnType(string parameter)
        {
            if (parameter == "JSON")
                return Json("<JSON>", JsonRequestBehavior.AllowGet);
            else if (parameter == "PartialView")
                return PartialView("<ViewName>");
            else
                return null;


        }

3

对于已经升级到MVC 3的人来说,这是一种不错的方式 使用MVC3和Json


1
你可以在MVC 2中使用本文介绍的同样技术。 - longhairedsi

2

灵活的方法根据请求生成不同的输出

public class AuctionsController : Controller
{
  public ActionResult Auction(long id)
  {
    var db = new DataContext();
    var auction = db.Auctions.Find(id);

    // Respond to AJAX requests
    if (Request.IsAjaxRequest())
      return PartialView("Auction", auction);

    // Respond to JSON requests
    if (Request.IsJsonRequest())
      return Json(auction);

    // Default to a "normal" view with layout
    return View("Auction", auction);
  }
}
Request.IsAjaxRequest()方法非常简单:它只是检查传入请求的HTTP标头,以查看X-Requested-With标头的值是否为XMLHttpRequest,大多数浏览器和AJAX框架都会自动添加该标头。
自定义扩展方法用于检查请求是否为json,以便我们可以从任何地方调用它,就像调用Request.IsAjaxRequest()扩展方法一样。
using System;
using System.Web;

public static class JsonRequestExtensions
{
  public static bool IsJsonRequest(this HttpRequestBase request)
  {
    return string.Equals(request["format"], "json");
  }
}

来源: https://www.safaribooksonline.com/library/view/programming-aspnet-mvc/9781449321932/ch06.html#_javascript_rendering

该页面讲解了在 ASP.NET MVC 中如何使用 JavaScript 渲染视图。你可以使用 jQuery 或纯 JavaScript 来完成这个过程。其中,使用 jQuery 会更加简单易用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接