限制每个操作的 HTTP 动词
限制每个操作的可用 HTTP 动词是一个好习惯吗?我的代码更干净,没有 [HttpGet]
、[HttpPost]
、[HttpPut]
或 [HttpDelete]
装饰每一个动作,但它也可能不太稳健或安全。我在许多教程或示例代码中都没有看到这样做,除非明确需要动词,例如有两个“创建”操作,其中 GET 版本返回新表单,POST 版本插入新记录。
Is it a good practice to limit the available HTTP verbs for every action? My code is cleaner without [HttpGet]
, [HttpPost]
, [HttpPut]
, or [HttpDelete]
decorating every action, but it might also be less robust or secure. I don't see this done in many tutorials or example code, unless the verb is explicitly required, like having two "Create" actions where the GET version returns a new form and the POST version inserts a new record.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我个人而言,我尝试尊重 RESTful 约定 并指定 HTTP 动词,但 GET 操作除外不修改服务器上的任何状态,从而允许使用任何 HTTP 动词调用它们。
Personally I try to respect RESTful conventions and specify the HTTP verb except for the GET actions which don't modify any state on the server thus allowing them to be invoked with any HTTP verb.
是的,我相信将您的操作限制为应处理的适当 HTTP 方法是一个很好的做法,这将使不良请求远离您的系统,降低可能攻击的有效性,改进代码文档,强制执行 RESTful设计等。
是的,使用
[HttpGet]
、[HttpPost]
.. 属性可能会使您的代码更难以阅读,特别是如果您还使用[OutputCache]
、[Authorize]
等。我对自定义
IActionInvoker
使用了一个小技巧,而不是使用属性,而是将 HTTP 方法添加到操作方法名称,例如:请注意,这不会更改操作名称,它们仍然是
LogOn
、LogOff
、Register
等。这里是代码:
Yes, I believe it's a good practice to limit your actions only to the appropriate HTTP method it's supposed to handle, this will keep bad requests out of your system, reduce the effectiveness of possible attacks, improve the documentation of your code, enforce a RESTful design, etc.
Yes, using the
[HttpGet]
,[HttpPost]
.. attributes can make your code harder to read, specially if you also use other attributes like[OutputCache]
,[Authorize]
, etc.I use a little trick with a custom
IActionInvoker
, instead of using attributes I prepend the HTTP method to the action method name, e.g.:Note that this doesn't change the action names, which are still
LogOn
,LogOff
,Register
, etc.Here's the code:
您不需要指定 HttpGet,您需要的所有其他内容
You don't need to specify the HttpGet, all others you do need