The most flexible place in ASP. Is the controller, where you can validate data, jump views, Access databases, and more. So, let's start with that.
1 controller is the class that inherits controllers, in general, after the class name will be added "Controller" word, ASP. NET MVC is a contract-first framework, where you can see a little bit of the clues, and then also related to other conventions
2 inside this controller, there are some methods, the public of which is shown and the return value of ActionResult is the so-called action, which can be understood as action, behavior, function, etc., and the icon is the login function.
3 There are various meta-tags in the controller, such as login verification, HttpPost method, etc. where [authorize] means that any action that accesses the controller needs to be logged in, otherwise it jumps to the login page, and we know an HTTP request, which is divided into the get and post methods, [ HttpPost] is used to specify that only post requests are allowed to be accessed.
4 sometimes in order to the needs of the program, will define a number of auxiliary methods, generally speaking, these methods are private
5 post method, typically use model to save data, this model can verify the legitimacy of data, such as login, user name is not allowed to be empty, password should be greater than 6 bits , you can use Modelstate.isvalidate to determine whether the data submitted is legal, illegal, and return.
6 in action can directly return a view, that is, return view (), and this view () method, overloaded with multiple methods, you can substitute the model mentioned above, so that when the data submitted in error, the original data will remain in the original page
7 said that the corresponding view name is not specified, but can still return normally, why? This is the agreement mentioned above. If you do not specify a view name, the system will go to the default view folder to find the view, look for the view will be in a certain sequence of traversal, if found, then return, or look down: The first search is the controller corresponding to the view folder, in fact, there is a view named register, So we can find it.
8 You can specify the name of the returned view so that you can still bring in the model that corresponds to the view
9 in this way, in the/views/account/folder can not find Noresource this view, how to do? The framework finds the public view folder/views/shared/, which is shown with this view, so it is not an error to return in the previous step, and the program will error if it cannot be found.
10 controller can also control other ways of jump, so you can specify to other controllers, shown as the exit login function, once exited, jump to the index of the home controller, that is, the first page
、
11 actually/home/index and/home are the same, because by default, only when the controller is provided, the framework will automatically look for the index method, which is another example of a contract priority
The 12 controller overview is introduced here first. I wish you a happy study!
I original, reproduced please indicate the source. Better visual typesetting please visit: HTTP://T.CN/RPJIST6