宁静的的WebAPI VS常规控制器 [英] Restful WebAPI VS Regular Controllers

查看:151
本文介绍了宁静的的WebAPI VS常规控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些R&安培; d什么似乎是一个非常令人困惑的话题,我也看了不少其他SO问题的,但我觉得我的问题可能是唯一的,足以保证我问。我们已经使用纯的WebAPI从未开发的应用程序。

I'm doing some R&D on what seems like a very confusing topic, I've also read quite a few of the other SO questions, but I feel my question might be unique enough to warrant me asking. We've never developed an app using pure WebAPI.

我们在试图写一个SPA风格的应用程序,在后端完全从前端code分离

假设我们的服务并不知道是谁在访问/消耗任何东西:

Assuming our service does not know anything about who is accessing/consuming it:

的WebAPI好像逻辑路线提供数据,而不是使用标准MVC控制器,并通过一个动作的结果为我们的数据,并将其转换成JSON。这至少对我来说似乎是一个MC设计...似乎很奇怪,并没有什么MVC是为那些。
(看妈妈......没有视图)

WebAPI seems like the logical route to serve data, as opposed to using the standard MVC controllers, and serving our data via an action result and converting it to JSON. This to me at least seems like an MC design... which seems odd, and not what MVC was meant for. (look mom... no view)

什么会被认为是正常的惯例在执行行动方面(y)的电话?

What would be considered normal convention in terms of performing action(y) calls?

我的感觉是,我的WebAPI的理解不正确。

我看的WebAPI的方式是,它意味着一个CRUD意义上被使用,但如果我想要做的事,如:InitialiseMonthEndPayment......我会需要创建的WebAPI控制器,​​叫InitialiseMonthEndPaymentController,然后执行POST ......似乎有点不可思议,而不是一个MVC控制器,在那里我可以只添加一个名为InitialisePayment的MonthEnd控制器上的新动作。

The way I perceive WebAPI, is that its meant to be used in a CRUD sense, but what if I want to do something like: "InitialiseMonthEndPayment".... Would I need to create a WebAPI controller, called InitialiseMonthEndPaymentController, and then perform a POST... Seems a bit weird, as opposed to a MVC controller where i can just add a new action on the MonthEnd controller called InitialisePayment.

还是这确实需要在设计方面心态转变?

Or does this require a mindset shift in terms of design?

有关此主题的任何进一步的联系将是非常有用的,因为我担心的是我们实现的东西,可能是怪异的可能变成一个编码/维护的关注以后?

Any further links on this topic will be really useful, as my fear is we implement something that might be weird an could turn into a coding/maintenance concern later on?

推荐答案

如果您打算让您的服务REST风格,控制器应该重新present的资源。在你的榜样,该资源付款,所以该控制器将被称为 PaymentController

If you are planning to make your services RESTful, the controller should represent a resource. In your example, the resource is Payment, so the controller would be called PaymentController.

您可以在同一个控制器的多POST方法。在你的情况,我会打电话的操作方法 PostMonthlyPayment 或类似的东西。该URL(路由)看起来像 http://server.com/api/payment/monthly 和身体(假设JSON)会是这样的:

You can have multiple POST methods in the same controller. In your scenario, I would call the action method PostMonthlyPayment or something similar. The URL (routing) would look like http://server.com/api/payment/monthly and the body (assuming JSON) would be something like:

{
   user: user@internet.com,
   month: 10,
   year: 2013,
   // any additional data
}

如果支付经历,一个好的做法是返回HTTP错误code 201和包含的URL支付GET方法的位置HTTP头。如果在身体的任何数据是错误的,返回一个错误code 400如果付款已经由用户,冲突code 409可以工作。

If the payment goes through, a good practice is to return HTTP error code 201 and the Location HTTP Header containing the URL to the payment GET method. If any data in the body is wrong, return a error code 400. If a payment had already been made by the user, conflict code 409 can work.

这篇关于宁静的的WebAPI VS常规控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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