简体   繁体   English

为什么在MVC应用程序中使用JSON?

[英]Why Using JSON in MVC application?

First i would like to apologize if this is a stupid question. 首先,如果这是一个愚蠢的问题,我想道歉。 While learning MVC i came a cross creating a strong type views based on model which for example will display all Users by 2 different functions, first will return a View passing a collection of users second will return a collection of JSON objects. 在学习MVC的过程中我创建了一个基于模型的强类型视图,例如将通过2个不同的函数显示所有用户,首先将返回一个视图,传递用户的集合,第二个将返回JSON对象的集合。 so my question is, as i'm not familiar with JSON and for me using Models are more clear, why then using JSON in MVC? 所以我的问题是,因为我不熟悉JSON,对我来说使用模型更清楚,为什么然后在MVC中使用JSON?

In short why using: 简而言之为什么使用:

var users = _db.Users
           .Where(r => r.Name.Contains(q))
           .Take(10)
           .Select(r => new { r.Name,r.LastName,r.Address.Country });
        return Json(users, JsonRequestBehavior.AllowGet);

In place of: 取代:

 var users= _db.Users
           .Where(r => r.Name.Contains(q))
           .Take(10);
            return View(users);

Maybe this is a bad code example, but why converting Models to Jason before passing them to a view, if we can directly pass the models. 也许这是一个糟糕的代码示例,但是为什么在将模型传递给视图之前将模型转换为Jason,如果我们可以直接传递模型。

If you want the call to return HTML that can be rendered in a browser, you would return a View, which basically takes the data and applies it to an HTML template. 如果您希望调用返回可以在浏览器中呈现的HTML,您将返回一个View,它基本上获取数据并将其应用于HTML模板。

If you want the call to return data only (most likely to be processed by an AJAX request), then you would return JSON. 如果您希望调用仅返回数据(最有可能由AJAX请求处理),那么您将返回JSON。

For the first code snippet, it is more likely that it is used for a ajax call to seamlessly load the data from the server to the client and perhaps keep refreshing it without reloading the page. 对于第一个代码片段,它更可能是用于ajax调用,以便将数据从服务器无缝加载到客户端,并且可能在不重新加载页面的情况下继续刷新它。 Also, the Users entity may have much more information than needed by the client, so the first example is reducing the information served and doesn't require you to create a new model to represent it. 此外, Users实体可能拥有的信息远远多于客户端所需的信息,因此第一个示例是减少所提供的信息,并且不需要您创建新模型来表示它。

The second example would be a controller action actually returning a view and that view is strongly typed. 第二个示例是实际返回视图的控制器操作,并且该视图是强类型的。

If you would make a project that uses the WebApi, you wouldn't return views, you would expect the client to ask for data and that the client renders it how it sees fit. 如果您要创建一个使用WebApi的项目,您将不会返回视图,您可能希望客户端请求数据,并且客户端会呈现它认为合适的方式。

The first is going to return JSON with a content type of application/json while the latter will return html with the content type text/html (after the appropriate View Rendering Engine has rendered the view in question) - they can be considered two different representations of the same resource. 第一个是返回带有application/json内容类型的application/json而后者将返回内容类型为text/html (在相应的View Rendering Engine渲染了相关视图之后) - 它们可以被认为是两种不同的表示形式相同的资源。 You may want to return JSON in response to a request made via AJAX and return only the data from the model that is required, but you may want to return HTML in response to a "normal" request. 您可能希望返回JSON以响应通过AJAX发出的请求,并仅返回所需模型中的数据,但您可能希望返回HTML以响应“正常”请求。

out of the box, ASP.NET MVC does not support Content Negotiation, that is, make a request with the Accept header set to a particular MIME type will likely not yield a response with the expected content type - controller actions will in the majority of cases be configured to return a resource in one particular content type (usually html). 开箱即用,ASP.NET MVC不支持内容协商,即,使用设置为特定MIME类型的AcceptAccept出请求可能不会产生具有预期内容类型的响应 - 控制器操作将占大多数case被配置为返回一种特定内容类型的资源(通常是html)。 ASP.NET Web Api on the other hand does support Content Negotiation out of the box. 另一方面,ASP.NET Web Api确实支持开箱即用的内容协商。

JSON(=仅数据)通常用于异步(AJAX)请求,因为它们可以通过Javascript轻松处理,而视图则是样式化的HTML响应,可供浏览器显示。

客户端库非常了解JSON,因此如果要解析结果并使用客户端框架/自定义脚本对其进行操作,那么JSON是更好的选择(AngularJs就是一个例子)。

In Addition to your question, when using JSON, first you convert Model to JSON but when you users post a request you might reconvert JSON to model in order to persist result to DB. 除了您的问题,在使用JSON时,首先将Model转换为JSON,但是当用户发布请求时,您可以将JSON重新转换为模型,以便将结果保存到DB。 :-) I :-) 一世

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

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