JSON操作的MVC命名约定

时间:2022-07-04 16:56:56

What naming convention is recommended when writing an MVC app that has both front-end and JSON paths to the required data?

在编写具有前端和JSON路径的MVC应用程序时,建议使用哪种命名约定?

For example, let's say the user of your site has "Things". They should be able to go to a page to view their things, but we also need a way to pull those things back as JSON on other pages. I've been able to think of several options but I'm not keen enough on any of them to proceed. Here's what I've got:

例如,假设您网站的用户有“事物”。他们应该能够访问页面来查看他们的内容,但我们还需要一种方法将这些内容作为JSON提取回其他页面。我已经能够想到几个选项,但我对其中任何一个都不够兴奋。这是我得到的:

  1. /things/list for UI, /json/things for JSON - this would require a JsonController which would end up serving different kinds of objects, thereby defeating any chance of entity separation before we even start.
  2. 用于UI的/ things / list,用于JSON的/ json /的东西 - 这将需要一个JsonController,它最终会提供不同类型的对象,从而在我们开始之前就不会有任何实体分离的机会。
  3. /things/list for UI, /things/list/json for JSON - probably my preferred option at the moment, but requires magic stringing (albeit just "json"). Also, if you also need a (string id) action signature for taking in some filter parameters or such, then you have the choice of adding an extra route or doing some dirty string splitting.
  4. 用于UI的/ things / list,用于JSON的/ things / list / json - 可能是我目前的首选选项,但需要魔术字符串(尽管只是“json”)。此外,如果您还需要(字符串ID)操作签名来获取某些过滤器参数等,那么您可以选择添加额外路由或执行一些脏字符串拆分。
  5. /account/mythings for UI, /things/list for JSON - a bit cleaner, but there might not always be a relevant controller that you could serve the "things" from. Plus, you're mixing entities again.
  6. / account / mythings for UI,/ things / list for JSON - 有点干净,但可能并不总是有一个相关的控制器,你可以提供“东西”。另外,你再次混合实体。

All and any suggestions welcome, thanks!

欢迎所有和任何建议,谢谢!

4 个解决方案

#1


15  

Arguably the path names could all be the same. You can examine the Accept header for the mime-type of your client's desired response, and then return an appropriate view based on what you find there:

可以说道路名称都可以是相同的。您可以检查客户端所需响应的mime类型的Accept标头,然后根据您在那里找到的内容返回相应的视图:

  • application/json: JSON View
  • application / json:JSON View
  • text/xml: XML View
  • text / xml:XML视图
  • text/plain, text/html: JSP View
  • text / plain,text / html:JSP视图

Browsers set this field to HTML; your JSON clients would simply set this field as appropriate.

浏览器将此字段设置为HTML;您的JSON客户端只需根据需要设置此字段。

#2


1  

It's highly unlikely that anyone would be bookmarking a URL that requests JSON so I think that it's not that important to keep the URL as clean. It's also likely to be programmatically generated, not hand entered. Given these, I'd consider adding it as a query parameter.

任何人都不太可能将请求JSON的URL加入书签,因此我认为保持URL干净并不重要。它也可能是以编程方式生成的,而不是手动输入的。鉴于这些,我会考虑将其添加为查询参数。

 /things/list  -- HTML
 /things/list?format=json  -- JSON 

This won't break your URLs if you do have ID parameters or need other parameters as well. It could also work with POSTs as well as GETs.

如果您有ID参数或需要其他参数,这不会破坏您的URL。它也适用于POST和GET。

/things/1  -- HTML for "thing 1"
/things/1?format=json -- JSON for "thing 1"

#3


1  

I use the convention of

我使用的约定

/things/list -- HTML
/things/_listpage -- AJAX

The rule is that all AJAXed actions/views have a leading underscore. This tells me that they are never called top-level, and usually have no master page associated. In this case, I keep the action under the same controller in order to share any associated logic.

规则是所有AJAXed操作/视图都有一个前导下划线。这告诉我他们从未被称为*,并且通常没有关联的母版页。在这种情况下,我将操作保持在同一个控制器下,以便共享任何相关的逻辑。

Typically in the list view I would have a

通常在列表视图中我会有一个

<% RenderAction("_listpage", "things", new {page = ViewData["CURRENT_PAGE"]}); %>

#4


-1  

I would recommend a slight variation/elaboration to the suggestion by @RedFilter

我建议对@RedFilter的建议稍作修改/细化

/things/list -- HTML
/things/_list -- return HTML help and examples (more for you than them).
/things/_list/schema -- schema info
/things/_list/json -- JSON format
/things/_list/xml -- XML format
/things/_list/csv -- csv format
/things/_list/tab -- tab deliminated format
/things/_list/wdsl -- implemented soap web service

etc.. I feel it is more extensible. It looks scary, but it is easy to pass the data contents through a decorator based on the format requested making a whole host of file formats available with practically just a few lines of code.

等等。我觉得它更具可扩展性。它看起来很可怕,但很容易通过装饰器传递数据内容,这是基于所要求的格式,只需几行代码即可提供大量文件格式。

Here is a crude conceptual example:

这是一个粗略的概念性例子:

public ActionResult _list(string id)
{
    string data = "";
    DataTable oDataTable = this.oDAO.Get("list"); // pretend data retrieval

    try{
        if(!String.IsNullOrEmpty(id)){
            data = this.oDecorator.FormatData(id,oDataTable);
            this.ContentTypeChange(id); // change application handler
        }else{
            data = this.GetHelp("_list");
        }           
    }catch{}
    ViewData["data"] = data;
    return View();
}

...help can be more of a feature list, technical examples, or whatever you want. Of course you can start with just having native JSON and add more data formats to your decorator as requirements grow which is nice. For many of my projects it starts as a pure json rest pull by an AJAX and tends to bloom into other formats that are needed based on site popularity, so I have found this robust enough to use in an enterprise setting for smaller projects that often grow big.

...帮助可以是更多功能列表,技术示例或任何您想要的。当然,您可以从拥有原生JSON开始,并在需求增长时为装饰器添加更多数据格式,这很好。对于我的许多项目来说,它最初是由AJAX开发的纯json休息,并且往往会根据网站流行度开展其他格式,因此我发现这足够强大,足以在企业环境中用于经常增长的小型项目大。

#1


15  

Arguably the path names could all be the same. You can examine the Accept header for the mime-type of your client's desired response, and then return an appropriate view based on what you find there:

可以说道路名称都可以是相同的。您可以检查客户端所需响应的mime类型的Accept标头,然后根据您在那里找到的内容返回相应的视图:

  • application/json: JSON View
  • application / json:JSON View
  • text/xml: XML View
  • text / xml:XML视图
  • text/plain, text/html: JSP View
  • text / plain,text / html:JSP视图

Browsers set this field to HTML; your JSON clients would simply set this field as appropriate.

浏览器将此字段设置为HTML;您的JSON客户端只需根据需要设置此字段。

#2


1  

It's highly unlikely that anyone would be bookmarking a URL that requests JSON so I think that it's not that important to keep the URL as clean. It's also likely to be programmatically generated, not hand entered. Given these, I'd consider adding it as a query parameter.

任何人都不太可能将请求JSON的URL加入书签,因此我认为保持URL干净并不重要。它也可能是以编程方式生成的,而不是手动输入的。鉴于这些,我会考虑将其添加为查询参数。

 /things/list  -- HTML
 /things/list?format=json  -- JSON 

This won't break your URLs if you do have ID parameters or need other parameters as well. It could also work with POSTs as well as GETs.

如果您有ID参数或需要其他参数,这不会破坏您的URL。它也适用于POST和GET。

/things/1  -- HTML for "thing 1"
/things/1?format=json -- JSON for "thing 1"

#3


1  

I use the convention of

我使用的约定

/things/list -- HTML
/things/_listpage -- AJAX

The rule is that all AJAXed actions/views have a leading underscore. This tells me that they are never called top-level, and usually have no master page associated. In this case, I keep the action under the same controller in order to share any associated logic.

规则是所有AJAXed操作/视图都有一个前导下划线。这告诉我他们从未被称为*,并且通常没有关联的母版页。在这种情况下,我将操作保持在同一个控制器下,以便共享任何相关的逻辑。

Typically in the list view I would have a

通常在列表视图中我会有一个

<% RenderAction("_listpage", "things", new {page = ViewData["CURRENT_PAGE"]}); %>

#4


-1  

I would recommend a slight variation/elaboration to the suggestion by @RedFilter

我建议对@RedFilter的建议稍作修改/细化

/things/list -- HTML
/things/_list -- return HTML help and examples (more for you than them).
/things/_list/schema -- schema info
/things/_list/json -- JSON format
/things/_list/xml -- XML format
/things/_list/csv -- csv format
/things/_list/tab -- tab deliminated format
/things/_list/wdsl -- implemented soap web service

etc.. I feel it is more extensible. It looks scary, but it is easy to pass the data contents through a decorator based on the format requested making a whole host of file formats available with practically just a few lines of code.

等等。我觉得它更具可扩展性。它看起来很可怕,但很容易通过装饰器传递数据内容,这是基于所要求的格式,只需几行代码即可提供大量文件格式。

Here is a crude conceptual example:

这是一个粗略的概念性例子:

public ActionResult _list(string id)
{
    string data = "";
    DataTable oDataTable = this.oDAO.Get("list"); // pretend data retrieval

    try{
        if(!String.IsNullOrEmpty(id)){
            data = this.oDecorator.FormatData(id,oDataTable);
            this.ContentTypeChange(id); // change application handler
        }else{
            data = this.GetHelp("_list");
        }           
    }catch{}
    ViewData["data"] = data;
    return View();
}

...help can be more of a feature list, technical examples, or whatever you want. Of course you can start with just having native JSON and add more data formats to your decorator as requirements grow which is nice. For many of my projects it starts as a pure json rest pull by an AJAX and tends to bloom into other formats that are needed based on site popularity, so I have found this robust enough to use in an enterprise setting for smaller projects that often grow big.

...帮助可以是更多功能列表,技术示例或任何您想要的。当然,您可以从拥有原生JSON开始,并在需求增长时为装饰器添加更多数据格式,这很好。对于我的许多项目来说,它最初是由AJAX开发的纯json休息,并且往往会根据网站流行度开展其他格式,因此我发现这足够强大,足以在企业环境中用于经常增长的小型项目大。