我应该在帖子或网址中存储_method = PUT / DELETE [英] Should I store _method=PUT/DELETE in the post or in the url

查看:236
本文介绍了我应该在帖子或网址中存储_method = PUT / DELETE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC构建一个RESTful Web应用程序,我计划通过POST隧道传递PUT和DELETE请求,因为这似乎是最实用的解决方法。

I'm using ASP.NET MVC to build a RESTful web application and I plan to tunnel PUT and DELETE requests through POST as it seems like the most pragmatic workaround.

我想知道的是,我应该通过这样的网址传递信息:

What I'd like to know is, should I tunnel the information through the url like this:

<form method='post' action='resource?_method=DELETE'>
    <!-- fields -->
</form>

或者我应该通过发布的表单数据进行隧道传输:

Or should I tunnel it through the posted form data like this:

<form method='post' action='resource'>
    <input type='hidden' name='_method' value='DELETE' />
    <!-- fields -->
</form>

每种优缺点有哪些?

编辑:我提出这个问题的原因之一是我在某个地方读到将这样的信息放在网址中是一件好事,因为帖子数据通常会丢失,但网址会挂起(在日志文件等) - 不幸的是它使网址看起来很难看

One of the reasons I asked the question is that I read somewhere that putting information like this in the url is a good thing as post data usually gets lost, but urls hang around (in log files, etc) - unfortunately it makes the url look ugly

推荐答案

这更多的是个人偏好。 Restful Web Services,OReilly,在某种程度上可以互换地描述。

It's more of personal preference. Restful Web Services, OReilly, describes both somewhat interchangeably.

话虽如此,出于程序员意图的原因,我更喜欢第一种方法。在休息时,当我查看代码时,我精神上阅读

That being said, I prefer the first method for reasons of programmer intent. In Rest, when I'm looking at the code, I mentally read

VERB http://someuri.com/someresource/id

动词与资源紧密相连。

The verb and the resource are close together.

使用PUT和DELETE,您必须使用类似于您所显示的解决方法。在第一个示例中,资源和动词在同一行上仍然靠近在一起。

With PUT and DELETE you have to use a workaround like the ones that you've shown. In the first example, the resource and the verb are still close together on the same line.

但是,在第二个示例中,资源分为两行。动词与资源ID位于同一行,但远离资源名称。这非常非常小,但对我来说,这使第二个例子的可读性降低。

In your second example, however, the resource is split into two lines. The verb is included on the same line as the resource id, but away from the resource name. It's very, very minor, but to me, that makes the second example less readable.

这篇关于我应该在帖子或网址中存储_method = PUT / DELETE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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