Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

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


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,

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


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900