Should I store _method=PUT/DELETE in the post or i

2019-04-09 13:53发布

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>

And what are the pros and cons of each?

EDIT: 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

4条回答
Melony?
2楼-- · 2019-04-09 14:07

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.

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.

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.

查看更多
甜甜的少女心
3楼-- · 2019-04-09 14:12

Have you seen this question? From what I understand, the x-http-method-override header is the preferred solution to this problem.

查看更多
smile是对你的礼貌
4楼-- · 2019-04-09 14:26

Not that I have, but shouldn't you use:

<form method="put" action="resource">
    <!-- fields -->
</form>

And / or

<form method="delete" action="resource">
    <!-- fields -->
</form>

...?

查看更多
Rolldiameter
5楼-- · 2019-04-09 14:26

They are equivalent.

Though if pressed I'd prefer sending it in the post myself, but it's subjective.

查看更多
登录 后发表回答