我们可以在MVC中的GET方法中INSERT / UPDATE / DELETE数据吗? [英] Can we INSERT/UPDATE/DELETE data in a GET method in MVC?

查看:87
本文介绍了我们可以在MVC中的GET方法中INSERT / UPDATE / DELETE数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道用GET动词修饰的MVC动作方法用于获取数据,类似的POST用于创建,PUT用于更新和删除以删除数据。



但我很想知道我可以使用GET动词修饰方法,但是使用ADO.Net或Entity Framework在action方法中执行CREATE,UPDATE或DELETE语句吗?



类似地,我用DELETE动词修饰一个方法,但是执行一个插入或更新数据的语句而不是删除它。



以上所有场景都会执行顺利/返回错误/不给出错误也不执行数据库操作?



基本上,我想知道这些HTTP动词是否有任何限制限制动作方法只执行有限的或特定类型的操作?



问候,

Shiva

I know that a MVC action method decorated with GET verb is used to get the data and similarly POST to create, PUT to update and DELETE to delete the data.

But I'm curious to know that can I decorate a method with GET verb but executes a CREATE, UPDATE or DELETE statement within the action method using ADO.Net or Entity Framework?

Similarly, I decorate a method with DELETE verb but executes a statement that inserts or updates the data rather than deleting it.

Will all above scenarios execute smoothly/ returns an error/ doesn't give error nor executes the database operation?

So essentially, I want to know that is there any restriction imposed by these HTTP verbs that restricts the action method to perform only limited or a particular type of operations?

Regards,
Shiva

推荐答案

运行aro需要HTTP谓词并且使用基于HTTP的API和其他需要一些HTTP动词来执行操作的相同协议,例如



The HTTP verbs are required to work around with HTTP based APIs and other same protocols which require some HTTP verb to perform actions, such as

GET http://www.example.com/users/id

DELETE http://www.example.com/users/id





从上面的URL中,您可以轻松判断哪一个会返回结果,哪一个会删除记录(取决于所使用的应用程序)。



现在,回答你的问题。虽然谁将实现实际的底层代码。因此,取决于您是否删除该记录以及是否返回记录或返回其他内容。这就是HTTP协议旨在为用户和开发人员创造直觉的方式。



除此之外,如果你想用这些动词做其他事情,你肯定可以。底层代码将按您的意愿执行。我可以编写action方法,它实际上从数据库中删除了用户。看下面,





From the above URLs, you can easily tell which one would return a result and which one would remove the record (depending on the application being used).

Now, coming to your question. Although it is you who would implement the actual underlying code. So, it depends on you whether you delete that record or not and whether to return the record or return something else. It is just the way HTTP protocol was designed to create an intuition for the users and developers.

Other than this, if you want to use these verbs to do something else, you surely can. The underlying code would execute as you want it to. I can write the action method, which actually deletes the user from the database. Look below,

[HttpGet]
public ActionResult Index() {
   // code to delete the user
   // Show the message
   ViewBag.Message = "User deleted";
   // Return the view
   return View();
}





虽然上面的代码是为了得到回复(不是用户或其他东西,只是回复 )但我添加了删除内容(或用户)的代码。没有错误,如果没有其他错误发生,也会删除记录。



实际上你应该遵循协议。因为,除非指定了动词,否则其他开发人员不知道你的函数会做什么。它只是消除了应用程序API的模糊性。否则,你肯定可以做任何事情。



您也可以从应用程序中删除该属性



Although the above code was to get the response (not a user or other stuff, just response) but I added the code to delete something (or the user). No error, and the record would also be deleted, provided no other error occurs.

The actual thing is that you should follow the protocol. Because, other developers have no idea what your function would do, unless the verb is specified. It is just to remove the ambiguity of the API of your application. Otherwise, you can surely do anything.

You can also remove that attribute from your application.


这篇关于我们可以在MVC中的GET方法中INSERT / UPDATE / DELETE数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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