After learning has been stuck for a long time, I am determined to continue. After too long, the fallen life also began to get tired. Continue to start learning MVC.
Directory organization:
Models Folder: model components can also store classes and object operation definitions for data access operations.
Views Folder: View component. The file types that can be stored include. aspx pages,. ascx controls, and. master pages.
Shared Folder: public part of the view component. You can store master pages, CSS styles, and other files.
Controllers Folder: Controller component.
You have registered the urlroutingmodule class in Web. config to parse URL routes.
<Add Name="Urlroutingmodule" Type="System. Web. Routing. urlroutingmodule, system. Web. Routing, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35" />
URL routing and related routing logic are set in the application_start () method in the global. asax file.
Public Static Void Registerroutes (routecollection routes) {routes. ignoreroute (" {Resource}. axd/{* pathinfo} "); Routes. maproute (" Default ", // Route name " {Controller}/{action}/{ID} ", // URL with Parameters New {Controller ="Home ", Action =" Index ", Id =" "} // Parameter defaults );} Protected Void Application_start () {registerroutes (routetable. routes );}}
Execution Process:
When the execution is based on ASP. net 3.5 MVC Framework mvcapplication website, according to the URL address in the browser, the URL address is first transmitted to the urlroutingmodule module, this module parses the URL address, and then select the relevant URL routing, and obtain the cashed ihttpcontext object to process the URL route. By default, the ihttpcontext object is the mvchandler object. Use the mvchandler object and select the relevant controller to process user requests.
Procedure:
In the mvcapplication Website Based on ASP. NET 3.5 MVC framework, each request page is mapped to the relevant methods in the corresponding controller. The Controller is responsible for returning the formulated content to the browser.Multiple pages can be mapped to different methods in the same controller.
In the ASP. NET 3.5 MVC Framework, the page-to-controller ing is implemented through the route table.ProgramThere is a path table. The path table is represented by the routetable. routes attribute.
Routes. ignoreroute ("{Resource}. axd/{* pathinfo}"); Routes. maproute ("Default",// Route name"{Controller}/{action}/{ID}",// URL with ParametersNew{Controller ="Home", Action ="Index", Id =""}// Parameter defaults);
When a request is based on ASP. net 3.5 MVC framework when the website page is in the web. in the config configuration file, the urlroutingmodule parses the URL and obtains the routedata object. Then, it creates the httphandler instantiation object mvchandler.
When executing mvchandler, call several processrequest () Methods to execute the processrequest () method to create a controller's instantiated object.
When the controller is executed, the execute () method in the method is called to call other methods through the reflection principle. The view () method is executed in the called method, return the content of the specified page to the browser ~
An example of adding, deleting, modifying, and querying:
Homecontroller:
[Handleerror] Public Class Homecontroller: controller {northwindentities northwind = New Northwindentities (); Public Actionresult index () {VAR model = northwind. categories. tolist (); Return View (model );} Public Actionresult about (){ Return View ();} [acceptverbs (httpverbs. Get)] Public Actionresult edit ( Int ID) {VAR model = northwind. categories. First (C => C. categoryid = ID ); Return View (model);} [acceptverbs (httpverbs. Post)] Public Actionresult edit ( Int ID, formcollection from) {VAR model = northwind. categories. First (C => C. categoryid = ID); updatemodel (model, New [] {"Categoryname "," Description "}); Northwind. savechanges (); Return Redirecttoaction (" Index ");} [Acceptverbs (httpverbs. Get)] Public Actionresult detail ( Int ID) {VAR model = northwind. categories. First (C => C. categoryid = ID ); Return View (model);} [acceptverbs (httpverbs. Get)] Public Actionresult create () {categories =New Categories (); Return View (categories );} Public Actionresult create ( Int Categoryid, formcollection form) {VAR model = northwind. categories. firstordefault (C => C. categoryid = categoryid ); If (Model = Null ) {Categories = New Categories (); updatemodel (categories, New [] {" Categoryname ","Description "}); Northwind. addtocategories (categories); northwind. savechanges (); Return Redirecttoaction (" Index ");} Else { Return Redirecttoaction (" Create ");}}}
I feel that it is very easy to get a glimpse of it, but it will take a lot of effort to make it practical in the future. Come on. Continue learning ............................................................