post 和 get 的操作名称和参数列表是否可能重复?
是否可以有两个具有相同名称和参数的操作,但一个是 post,另一个是 get?例如 Delete(id)
和 [HttpPost]Delete(id)
...我收到一条错误,指出这是不允许的...
is it possible to have 2 actions with the same name and parameters but one's a post, the other a get? e.g Delete(id)
and [HttpPost]Delete(id)
...i get an error saying that this is not allowed...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的。只需在一个操作上使用 ActionName 属性:
Yes, it's possible. Just use ActionName attribute on one action:
您收到不允许的错误的原因是因为 C# 本身会变得混乱。虽然在 MVC 中,您可以添加属性来指定函数是 HttpGet 还是 HttpPost,但这无助于 C# 确定其中之一或另一个之间的差异。为了让两个函数具有完全相同的名称,参数列表需要不同。
正如 frennky 指出的,ActionName 属性在 MVC 中起作用,因为 MVC 使用别名作为确定要调用哪个操作的过程的一部分(以及属性,但不包括参数)。
附带说明一下,最好不要对 GET 请求执行删除操作。您不希望爬虫或其他机器人意外点击错误的链接:P
The reason you get the error that it is not allowed is because C# itself gets confused. While in MVC you can add attributes to specify whether a function is HttpGet or HttpPost, that doesn't help C# determine the difference between one or the other. In order to have 2 functions with exactly the same name, the parameter list needs to be different.
As frennky pointed out, the ActionName attribute works in MVC because MVC uses aliases as part of the process for determining which action to call (along with attributes, but not parameters).
As a side note, it's probably best not to have a Delete action on a GET request. You don't want a crawler or some other bot accidently hitting the wrong link :P