In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to understand the basic knowledge of ASP.NET MVC, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Editors recommend "ASP.NET MVC Video tutorial"
One penguin first found that the earth was warming and the iceberg was melting, and came back to tell the other penguins, but none of the penguins wanted to believe it, because the penguins felt that they were living a good life now and didn't want to bother to think about how to change and when the change would come. so they still do routine work every day and are unwilling to study other people's opinions. Sometimes people resist change, but they just don't want to change, but technicians who are willing to accept new ideas usually live longer.
Often on the Internet, I see netizens mistakenly think that MVC and 3-Tier (three-tier architecture) are similar things.
But the two are essentially completely different concepts, as follows:
The architecture of 3-Tier or N-Tier, and the middle tier may contain EJB, Enterprise JavaBeans (J2EE), or .NET Enterprise Services, Remoting, WCF:
Client (WebPage)-[BLL (business logic layer)-DAL (data access layer)] (Web Server)-data source or database
Client (WebPage)-[BLL (business logic layer)-DAL (data access layer)] (Web Server + Application Server)-data source or database
MVC is:
◆ Model-contains BLL, DAL. Both View and Controller depend on Model, but Model depends on neither View nor Controller, which is one of the main advantages of separation
◆ View (View)-only responsible for generating output (UI). In the case of ASP.NET MVC, there are no more .aspx.cs files like Code-behind, and there are no runat= "server" tags, form tags, control declarations, and event handling.
◆ Controller (controller)-controls the workflow, operational logic, error handling, identity authentication and authorization, user input data validation,... Wait
Generally speaking, Microsoft's ASP.NET MVC Framework is to make ASP.NET more suitable for medium and large-scale projects, to uniformly plan and control the flow of the entire website system (handled with Controller), and to more effectively divide the development, maintain in the future (avoid repeated custom functions of the same function in many WebForm), control the impact of a function on the system as a whole after modification, and cut different functions more clearly. Allowing technicians with different expertise to perform their own duties also improves the readability of the code by the way, making it easier to test (TDD, test-driven development) (reducing the amount of code directly tied to the user interface) [4], and achieving "loose coupling (loosely coupled)", making components easy to replace and reuse. But also because of this, ASP.NET programmers must first change the past, put a lot of business logic, input validation, page switching … Other miscellaneous functions, all written in Code-Behind (aspx.cs) in the old habits.
ASP.NET MVC provides a framework that enables you to easily implement the model-view-controller (MVC) pattern for Web applications. This pattern lets you separate applications into loosely coupled, pluggable components for application design, processing logic, and display. ASP.NET MVC also greatly facilitates test-driven development (TDD).
In fact, MVC Pattern has been invented for more than 20 years, and its concept and framework (MVP) have been in practice for many years in some Framework of Java / J2EE / Struts. The structure of the MVC schema is as follows [4]:
◆ View (JSP) is only used to generate output, and does not involve access to data sources, event handling, various logic processing, and computing work. Therefore, it is more suitable for the division of labor of large projects, leaving this floor entirely to artists (rather than programmers, artists and typesetting). This layer is like the .aspx foreground page in ASP.NET, that is, UI (User Interface).
◆ Model (JavaBean, EJB components) is used to store independent and reusable components, including access to data sources (databases), business logic code, and should be completely cut from View in order to retain the flexibility of the system when it is expanded or rewritten in the future. This layer is like a custom class, DataSet (.xsd), TableAdapter, … in the App_Code folder in ASP.NET. Wait.
◆ Controller (Java Servlet) is used to control the "flow" of the whole website, is responsible for coordinating the transfer and diversion of the process between View and Model, and also manages and assigns which file to receive the "request" put forward by the user, that is, it decides which aspx file to present to the user. When the user sends out the request from the browser (for example, click the control on the page with the mouse, or click the Button submit after entering the data, enter the URL URL, click hyperlink … Etc.), some method defined in Controller will determine which operation logic in the Model to process, and then determine which View to send the processing result back to for display.
In addition, Controller can also include error handling, authorization, input validation. And other functions of the code, centralized unified processing, in order to avoid a large number of repetitive code in the traditional WebForm. But to put it bluntly, Controller is really just a custom class with some Attribute (features). And Controller is also the part that ASP.NET lacks at present.
Figure 1 how the MVC (Model 2) architecture works, which corresponds to the ASP.NET MVC project in figure 2 below
In fact, in the website development architecture often referenced by Java / JSP / JavaEE, it can be divided into Model 1 and Model 2. Model 1 can also be divided into two to three categories, as follows:
◆ * is a combination of HTML and .NET (Java) code, commonly known as spaghetti, such as the ancient ASP. The most dirty problem with Inline code is that the code is not readable and difficult to maintain.
The second type of ◆ is an one-to-one Code-Behind code (.aspx.cs) with .aspx to access the database directly, that is, a two-tier architecture. But this disadvantage is that the code is difficult to reuse, and because the logic has been written in a fixed page, it will cause difficulties for the system to expand and maintain in the future.
The third kind of ◆ is to access the database or do business logic operations (JSP + JavaBean) through custom class libraries or custom classes or components in the App_Code folder. However, this approach still lacks the unified control of "flow". As a result, every ASPX (JSP) has to verify the user's identity, verify the parameters of request, deal with Session, make exceptions, and even include the coding principles and language family settings in View, which have to be dealt with in the corresponding Code-Behind of each ASPX, so it is not suitable for the development, expansion and maintenance of large-scale systems. Although this architecture can also achieve virtual three-tier or multi-tier architecture, it is also the limit of the current ASP.NET.
As for Model 2, commonly known as MVC Pattern, it adds a part of Controller to put processes and events under central control, which can not only make the operation flow of the whole system more accurate and effectively cut open the work of each layer, but also prevent Code-Behind in View from dealing with database access and business logic operations in Model, and there is no need to repeatedly write the code of "process transfer and redirection" on each page. Instead, the central Controller program code (action method) will be under unified control.
But MVC architecture also has its shortcomings, for example, developers need to spend additional time to change ideas and learn a certain Framework, especially .NET developers, because in the past there is no concept of Controller unified coordination process, they are bound to get used to it and rewrite a lot of code that was originally written in various pages in Controller. And when designing the stage, the system must first coordinate the format and practice of data exchange among various classes and objects, so it is bound to lengthen the analysis and planning schedule of the system in advance. However, if a ready-made framework such as Java Struts or ASP.NET MVC Framework can be applied, it is expected to achieve twice the result with half the effort when developing large-scale systems in the future.
To develop ASP.NET MVC, you must have Visual Studio 2008 + SP1 and download the MVC suite (Microsoft finally released version 2008 in April 2009 after at least two years of research and development) at the following URL:
ASP.NET MVC 1.0 download (2009-04-09):
Http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6- 35e00103cb4b&displaylang=en
After the installation is complete, there will be an extra option of "ASP.NET MVC Web Application" when you create a new project. After you create a default MVC project, its structure in the VS solution is shown in the following figure:
Figure 2 ASPX, ASCX and MasterPage in View are used for display only. There is no Code-Behind file (aspx.cs) configured by default.
As shown in figure 2 above, the ASP.NET MVC project automatically generates three folders for storing MVC files, two Controller classes that control the flow, multiple View pages without Code-Behind, and a Global.asax.cs that defines the Routing rules.
As a matter of fact, when I tried the ASP.NET MVC Beta version a year ago, the View still came with the Code-Behind file by default, but it was empty and there was only one line of comments:
!-Please do not delete this file. It is used to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request to the server. --
In the official version released not long ago, View does not configure Code-Behind files by default. In addition, View in ASP.NET MVC, in order to make the project development more clearly cut, there are no page initialization and loading methods, no event handlers, nothing except the base class declaration, which is declared as System.Web.Mvc.ViewPage rather than the System.Web.UI.Page of WebForm in the past.
The newly created ASP.NET MVC project can be executed by pressing F5 directly, as shown in figure 3 below. However, because the View of MVC requires Controller to run before it is displayed (see figure 1), if you try to set a .aspx in View to "start page", when you press F5, "HTTP 404. 4." The resource cannot be found. "error, that is, if you want to navigate directly to the page will not work.
Figure 3 the home page of the ASP.NET MVC project
You will find that the URL in the browser looks like this, and the URL is not a specific .aspx extension:
Http://localhost: port number / Home/index
Http://localhost: port number
In fact, the content of this page is to capture the contents of the Index.aspx under the Views/Home folder to render. It is configured by Global.asax.cs and uses "URL rewrite (Url Routing)" to allow users to access the site according to your own defined rules [14].
On the right side of the first page of figure 3, there will be two hyperlink, which can be directed to the "LogOn.aspx" and "About.aspx" pages, respectively; but which page hyperlink should direct is not written in the View page (there is no Code-Behind), but is uniformly defined by Global.asax.cs to resolve which Controller custom class should be thrown to handle when the browser receives a URL, a form, or any request. Then the designated Controller (such as the automatically generated HomeController.cs by default in figure 2 above), one or more System.Web.Mvc.ActionResult classes defined in it, and the View method (terminology called action method) are used to set which View (page) to return the UI render to, that is, as mentioned at the beginning of this article (see figure 1 of this post), the Model 2 architecture is a Controller file that controls the flow and page orientation of the entire ASP.NET system. Instead of writing in various aspx.cs files.
The following is part of the code of Global.asax.cs. Netizens can read it with reference to the structure of Controller and View in figure 2 above:
Using System.Web.Mvc
Using System.Web.Routing
Public class MvcApplication: System.Web.HttpApplication
{
Public static void RegisterRoutes (RouteCollection routes)
{
Routes.IgnoreRoute ("{resource} .axd / {* pathInfo}")
/ / Anonymous Type (Anonymous Type) syntax of C # 3.0
Routes.MapRoute (
"Default", / / Route name
"{controller} / {action} / {id}", / / URL with parameters (controller name / operation name / ID parameter)
New {controller = "Home", action = "Index", id = ""} / / Parameter defaults
);
}
}
The three parameters in the MapRoute method represent routes, route name, and URL. The * * parameter "Default" represents the Default.aspx in the project (not located in the View folder). The purpose of this URL Routing is to solve the problem that the file cannot be found when the user accesses the domain name directly, so use this method to change the home page to Routing to Home/Index. So when you type in the browser's address bar:
Http://localhost: port number / Default.aspx
Or
Http://localhost: port number
It will also lead to Views/Home/Index.aspx in figure 2.
System.Web.Mvc Namespace (MSDN Library, Chinese version is not available):
Http://msdn.microsoft.com/en- us/library/system.web.mvc.aspx
RouteCollectionExtensions.MapRoute Method:
Http://msdn.microsoft.com/en- us/library/dd470521.aspx
ASP.NET MVC Framework (Part 1) (blog released by ScottGu in November 2007, which shows ASP.NET MVC in the Beta version):
Http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
In the home page of figure 3, when the mouse moves over the hyperlink on the right, the page name of the actual link (some .aspx) is not directly displayed in the address bar of the browser and in the information bar below the browser, because the operation of request "redirection" is handled uniformly by Controller, that is, it is specified by the actionName of the System.Web.Mvc.ActionResult class configured in the HomeController.cs in figure 2. Therefore, the URL of the browser will still maintain the Html.ActionLink binding in Site.Master (MasterPage), and the configured actionName (that is, the second parameter "Index" and "About" of the following two lines of code) will not show the page name of an .aspx directly in the address bar as in the past ASP.NET 1.x / 2.0.
The following is part of the code for Site.master:
In addition to controlling the process shift of the entire website, Controller can actually avoid a lot of code duplication, that is, it can handle some functions that can be "shared" by the system, such as authentication user identity, input verification (validate), Session management, or, as in shopping websites, when items in the shopping cart are added, modified, or removed, any user has the same "common logic" processing. And to check out, to do the calculation of shopping quantity and unit price multiplication, or to add a Collection data structure object that stores shopping details to Session,*** and then redirect to a page (View).
Summary:
This post is only for. Net technicians as an introduction to MVC Pattern and ASP.NET MVC Framework. If you are interested in in-depth research, you can refer to the "relevant documents" at the bottom of this post. In addition, there are many related posts in the blog park and on the Internet. As for the general concept of MVC, you can also refer to the network theory forum of JSP / Struts / J2EE, related books, or "compound pattern (Compound Pattern)" in Design Patterns (Design pattern) [15].
As mentioned in MSDN Magazine [7], ASP.NET MVC is not used to replace the traditional ASP.NET WebForm, both have their own advantages and disadvantages, and the future ASP.NET MVC may continue to improve on UI, control drag, and Routing. However, as a technician, * * was able to clarify the characteristics, advantages and disadvantages of both early [4] [6] to assess whether to go deep into learning or project introduction in the future, and to think about why Microsoft launched this framework. And it took at least two years to develop it very carefully.
On how to understand the basic knowledge of ASP.NET MVC is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.