In this part of the tutorial, we will discuss the automatically generated details and delete methods.
Querying details and Delete methods
Open the movie controller and view the details Method.
Public ActionResult Details (int? id) {if (id = = Null) {return new Httpstatuscoderesult (httpstatuscode.badrequest);} Movie movie = Db. Movies.find (id); If (movie = = Null) {return httpnotfound ();} return View (movie);}
The MVC Scaffolding engine adds a comment indicating that in the invocation of the HTTP request method, The GET request has three URL segments, a movies controller, details method, and ID Value.
Code first allows you to easily use the Find method to search for Data. An important security feature is built into the Method. Method first verify Find
that the method has found a movie and then executes other Code. For example, hackers can change http://localhost:xxxx/Movies/Details/1 to http://localhost:xxxx/Movies/Details/12345 (or some other value, which does not represent the value of the actual movie), causing the link URL to appear incorrectly. If you do not detect whether the movie was found, null movie causes a data Error.
View Delete
and DeleteConfirmed
methods.
GET:/movies/delete/5public actionresult Delete (int? id) {if (id = = Null) {return new Httpstatuscoderesult ( httpstatuscode.badrequest);} Movie movie = Db. Movies.find (id); If (movie = = Null) {return httpnotfound ();} return View (movie);} POST:/movies/delete/5[httppost, ActionName ("Delete")][validateantiforgerytoken]public ActionResult deleteconfirmed (int id) {movie movie = Db. Movies.find (id);d B. Movies.remove (movie);d B. SaveChanges (); return redirecttoaction ("Index");}
Note that Delete
the HTTP Get
method does not delete the specified movie, it returns the view where the movie was deleted, and you can submit () delete the movie in this view HttpPost
. If you use a GET request to perform a delete operation (or to perform an edit operation, create an action, or any other action that changes the data), a security vulnerability is Created. For more information on this, see Stephen • Walter's blog asp. net MVC Tip #46-don ' t use Delete Links because they create Security Holes.
A method that names the method that deletes the data HttpPost
as a unique signature or name DeleteConfirmed
. The signatures of these two methods are as Follows:
GET:/movies/delete/5public actionresult Delete (int? id)////POST:/movies/delete/5[httppost, actionname ("Delete")] Public ActionResult deleteconfirmed (int id)
When the common language runtime (CLR) overloads a method, the method requires a unique signature (the same method name but a different parameter list). however, Here you need two ways to delete-a get method and a post method that both have the same signature. (they all need to accept an integer as an argument).
There are several ways to address this. One is to use a different method Name. This is the method used by the framework code in the previous Example. however, this poses a small problem: asp. net will map part of the URL by name to the action method, and if you rename the method, usually routing will not find the Method. The workaround is what you see in the example, ActionName("Delete")
adding attributes to the DeleteConfirmed
method. This effectively executes the URL mapping of the routing system, so that a URL that contains the /delete/ POST request will find the DeleteConfirmed
method.
Another common way to avoid methods that have the same name and signature is to artificially change the post method, including signatures that do not use Parameters. For example, some developers who add parameter types Formcollection,formcollection are passed to the POST method and do not use this parameter at All:
Public ActionResult Delete (formcollection fcnotused, int id = 0) {movie movie = Db. Movies.find (id); If (movie = = Null) {return httpnotfound ();} Db. Movies.remove (movie);d B. SaveChanges (); return redirecttoaction ("Index");}
Summary
You now have a complete asp. net MVC application and store the data in the local DB database. You can create, read, update, delete, and search Movies.
Next
After you build and test a Web application, the next step is to give it to someone else to make it accessible over the Internet. To do this, you need to deploy it to a web Host. You can deploy up to 10 Web sites through Microsoft's free Windows Azure trial Account. I suggest you?? Next please follow my tutorial deploy a Secure asp. net MVC app with membership, OAuth, and SQL Database to a Windows Azure Web Site for a more in-depth understanding of how to Deploy. In addition, there is a good tutorial for Tom Dykstra's Intermediate creating an Entity Framework Data Model for an asp. net MVC Application. StackOverflow and asp. net MVC forums. Whether it's the knowledge presented in this section or the mid-level tutorials of Tom Dykstra's are designed to help you better develop mvc, you can develop with some development tools. ComponentOne Studio ASP is a control package for the MVC platform that integrates seamlessly with Visual Studio and is fully compatible with MVC6 and asp. net 5.0, which will significantly increase Productivity.
A good place to ask Questions: Stackoverflow's asp. net MVC forum or GCDN Web software development discussion Area. Please follow our blog so you can get the updated information stream for the latest tutorials.
Any comments, welcome feedback.
-----------------------------------------------------------------------------------------
The 12 articles in the asp. net MVC 5 Getting Started Guide are summarized below:
1. ASP 5-start MVC 5 Tour
2. asp. NET MVC 5-controller
3. asp. NET MVC 5-view
4. ASP 5-pass data from the controller to the view
5. asp. 5-add A model
6. ASP 5-create A connection string (Connection String) and use SQL Server LocalDB
7. Asp. 5-access The data model from the controller
8. Asp. 5-validation Editing method and edit view
9. ASP 5-add new fields to movie tables and models
asp. Net MVC 5-add a validator to the data model
asp. Net MVC 5-query details and Delete methods
asp. NET MVC 5-create an app with the Wijmo MVC 5 template for 1 minutes
Go Asp. 5-query details and Delete methods