In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand. Net MVC Framework in NET Web Development". The explanation in this article is simple and clear and easy to learn and understand. please follow the editor's train of thought to study and learn "how to understand. Net MVC Framework in NET Web Development"!
MVC concept
MVC is an architectural design pattern that is mainly used in graphical user interface (GUI) applications. So what is MVC? MVC consists of three parts: Model (model), View (view) and Controller (controller).
Model is the data model of the application. No application can do without data, which can be stored in a database, in disk files, or even in memory. Model is the abstraction of these data. No matter what form the data is stored, the application is always able to operate on the data through Model, regardless of the storage form of the data. Data entity class is a commonly used Model. For example, a customer management application uses a database to store customer data. There is a customer table Customer in the database table, and a data entity class Customer is generally created in the corresponding program to correspond to it. This entity class is even the Model of the customer table.
View is the interface of the application. Users use View to operate the application and complete the interaction with the program. View provides a visual interface to display the data defined in Model. The user manipulates the data through View and returns the results of the operation on the Model data to the user. In a desktop application, a View may be one or more Windows forms. In Web applications, View is made up of a series of web pages, which are .aspx pages in ASP.NET sites.
Controller defines the application logic of the program. The user sends the operation command to Controller through View, and the Controller updates the data defined by Model according to the programming logic, and returns the operation result to the user through View.
History of MVC
The concept of MVC was first put forward by American professor Trygve Reenskaug in 1979. In 1988, the design pattern of MVC was formally put forward in the book A Cookbook for Using the Model-View-Controller User Interface Paradigm in Smalltalk-80. With the rapid development and popularization of Microsoft Windows operating system, graphical user interface applications have gradually become the mainstream. MVC framework has appeared in many programming languages to facilitate developers to use this pattern to design applications. Most of these frameworks are targeted at Web applications.
Implementation of MVC Design pattern in .NET Web Development
CodeBehind technology is used in ASP.NET 1.x, which completely ends the nightmare of traditional ASP program development: program logic is mixed with HTML interface elements. CodeBehind technology separates the .aspx file representing the program interface (View) from the logic (Controller) code .vb / .cs file, which is a kind of MVC design. CodeBeside technology has appeared in ASP.NET 2.0, that is, a .aspx file can have multiple .vb / .cs files, which facilitates the further separation of the interface from the logic code.
In March 2008, Microsoft released the MVC framework (Preview 2 version) for ASP.NET 3.5. This is a real ASP.NET MVC framework. The framework can be said to be a "subversion" of the Web Form-based application development approach that developers were familiar with before. The change can be described as "shocking":
1. Using URL Routing technology: the URL of a Web program no longer points to a specific physical page .aspx, but to a method of a Controller. For a typical MVC-based program, its URL might be as follows:
Http://www.mysite.com/Customer/Index
The URL of a program that uses the MVC architecture does not have to have a file extension. The Customer in the above URL is the name of Controller. Index is a method name defined by Customer.
2. The interface of the Web program. ASPX no longer uses the server-side Form:
Then the Postback related to the server-side Form and the events of the page life cycle no longer exist.
3. There is no more View State on the page. View State will not be able to store program state information under MVC.
4. Server control events that depend on server-side Form are no longer provided, and Button_Clicked events that developers are familiar with will no longer be needed under MVC.
NET MVC example
After installing ASP.NET MVC Preview 2, a new project template "ASP.NET MVC Web Application" is added to VS2008, as shown in the following figure
After the new project is created, the file structure of the project automatically generated by VS2008 is as follows. Each of the three components of MVC has a folder to store its own program files.
The URL Routing mentioned earlier is set in Global.asax.cs:
The copy code is as follows:
Public class GlobalApplication: System.Web.HttpApplication
{
Public static void RegisterRoutes (RouteCollection routes)
{
/ / Note: IIS versions below IIS7 need to set the URL format to "{controller} .mvc / {action} / {id}" to enable.
Routes.Add (new Route ("{controller} .mvc / {action} / {id}", new MvcRouteHandler ())
{
Defaults = new RouteValueDictionary (new {action = "Index", id = ""})
}); / / set URL Routing format
Routes.Add (new Route ("Default.aspx", new MvcRouteHandler ())
{
Defaults = new RouteValueDictionary (new {controller = "Customer", action = "Index", id = ""})
}); / / set the default URL pointing to the Index method of Customer Controller
}
Protected void Application_Start (object sender, EventArgs e)
{
RegisterRoutes (RouteTable.Routes)
}
}
[code 1]: Global.asax.cs
Here's how to implement Model, Controller and View of Customer:
Model: under the Model folder in the project, create a new "Linq to SQL Classes" and drag and drop the Customer table from the Northwind database into its design view. This completes the Model corresponding to Customer. As shown in figure 4
Controller: under the Controller folder in the project, create a new "MVC Controller Class" and name it CustomerContoller.cs. Add a public method Index to this class, this method and the method mapped for the default URL set in Global.asax.cs.
The copy code is as follows:
Public class CustomerController: Controller
{
Public void Index (string id)
{
Northwind.Models.NorthwindDataContext dc = new Northwind.Models.NorthwindDataContext ()
IList customers = dc.Customers.Take (10) .ToList (); / / fetch 10 Customer records in the database
RenderView ("Index", customers); / / returns Index View
}
}
[code 2]: CustomerController.cs
View: the code for the Index method above indicates that after the Index method of CustomerContoller executes, it needs to return a View named Index to present the data to the user. Let's add this Index View: in the project's View file, create a new subfolder Customer. View related to Customer Controller will be saved in this folder. Create a new "MVC View Class" and name it Index.aspx. In the previous RenderView ("Index", customers) method, the customers parameter, which is of type IList, is the data that Controller passes to View. To facilitate the use of this strongly typed data in View, View.aspx.cs uses the following code: pay attention to the bold part
The copy code is as follows:
Public partial class Index: ViewPage
{
}
[code 3]: Index.aspx.cs
The View.aspx code is as follows: the type of the member variable ViewData and the IList type mentioned above.
The copy code is as follows:
Edit
Customer ID
Company Name
Contact Name
Contact Title
Edit
[code 4]: Index.aspx
Let's implement the Edit method of Customer Controller. Add the following code to CustomerController.cs:
The copy code is as follows:
Public void Edit (string id)
{
Northwind.Models.NorthwindDataContext dc = new Northwind.Models.NorthwindDataContext ()
Customer c = dc.Customers.Single (cus = > cus.CustomerID = = id); / / fetch a Customer record corresponding to the parameter id from the database
RenderView ("Edit", c); / / returns Edit View
[code 5]: Edit method in CustomerController.cs
Accordingly, under the View/Customer/ folder in the project, add Edit View Edit.aspx:
The copy code is as follows:
Public partial class Edit: ViewPage
{
}
[code 6]: Edit.aspx.cs
The copy code is as follows:
Cc.Update (ViewData.CustomerID)) {% >
Customer ID:
Company Nmae:
Contact Name:
Contact Title:
[code 7]: Edit.aspx
Html, a helper class in the MVC framework, is used in Code 7. This class can produce interface elements commonly used in View, such as html form, text input boxes, and so on.
Here's how to implement the Update method of CustomerController:
The copy code is as follows:
Public void Update (string id)
{
Northwind.Models.NorthwindDataContext dc = new NorthwindDataContext ()
/ / fetch a Customer record corresponding to the parameter id from the database:
Customer cust = dc.Customers.Single (c = > c.CustomerID = = id)
/ / assign the user's changes in Edit View to the cust object:
BindingHelperExtensions.UpdateFrom (cust, Request.Form)
Dc.SubmitChanges ()
RedirectToAction ("Index"); / / Jump to Index View
}
[code 8]: Update method in CustomerController.cs
The above code realizes the list, editing and updating functions of Customer through the ASP.NET MVC framework, it can be seen that MVC separates the three parts of the application Model, View and Controller "elegantly", and really realizes the flexible architecture of high cohesion and low coupling, which greatly reduces the complexity of the program and improves the scalability and reusability. The impact of this framework on Web development is not only a change in technology, but also a change in Web programming ideas-Web programs are no longer a collection of functional pages, but a collection of functional units controlled by Controller. Web programs are more like a group of "API" opened to the outside world through its URL.
Thank you for your reading, the above is "how to understand. Net MVC Framework in NET Web Development". After the study of this article, I believe you have a deeper understanding of how to understand. NET Web development. Net MVC Framework in the development of this problem, the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.