Learn ASP. net mvc (first) Theory

Source: Internet
Author: User

Learning ASP. net mvc Architecture is a very good thing. I think everyone has already learned about it. This article is the basic knowledge, which can be summarized by myself and bypassed by experts.
MVC
Three roles:
-- Model: component used to store data
-- View: a component that displays content based on Model data.
-- Controller: receives and processes user commands (operates on the Model), selects a View, and outputs the content.
The Controller references the View, but the View does not know the existence of the Controller. Both Controller and View reference the Model in one way.
MVC variants: Observer mode and MVP mode.

MVC and L3 Architecture:
A three-tier architecture is an architecture model with obvious hierarchical relationships, while MVC is a presentation model. The three elements have no hierarchical relationships, only the collaboration between components.

MVC and WebForms
WebForms advocates the abstraction of Web development into a component model, which is easy to use and easy to get started. However, it is too encapsulated and difficult to use. There are complex internal lifecycles and event processing.

Advantages of ASP. NET MVC
1. Direct, no concept of too many controls
2. Easy unit test of interface logic
3. Easy cooperation between front-end and back-end personnel
....

ASP. net mvc component collaboration (Basic Process)
URL --> Controller (a method Action will be executed) --> get a Model object from the database --> pass the Model as ViewData to View (Detail. aspx ).
Main steps: URL Routing, Controller & Action Executing, View Rendering

URL Routing:
ASP. net mvc is URL-driven. The Controller name, Action name, and other parameters are determined from the URL according to the rules specified in the program.
 

System. Web. Routing. dll. Components independent of the ASP. net mvc framework can be used in any ASP. NET application. The function is to convert the URL to RouteData and other data.
Usage:
1>. Configure the UrlRoutingModule.
2> Add Rules when Application_Start. Pay attention to the locking problem here (because Web applications are inherently multi-threaded ).
3>. Rule Order.
4>. RouteExistingFiles properties.

Define in the Global. asax file:

// URL rule Definition
Public static void RegisterRoutes (RouteCollection routes)
{
Routes. MapRoute (
"Default ",
"{Controller}/{action}/{id }",
New {action = "Index", id = 0 },
New {controller = @ "^ \ w +", id = @ "\ d + "});

// Car/bnw. abc
Routes. MapRoute (
"Car ",
"Car/{make}. {model }",
New {controller = "car", action = "Index "},
New {make = @ "(acural | bnw )"});
}

Protect void Application_Start ()
{
RegisterRoutes (RouteTable. Routes );
RouteTable. Routes. RouteExistingFiles = true;
}

If the URL accesses a physical file on the disk, it will not be URL Routing by default. You can use RouteTable. Routes. RouteExistingFiles = true; to avoid this.

The difference between Routing and URL Rewrite:
URL Rewrite: changes the URL processed by the Program (ASP. NET WebFrom)
URL Route: select and execute according to URL rules (MVC)

Controller & Action
The Controller class and Action method determined by URL Routing. The Action executes the presentation logic. You can operate the Model (Execution business logic) based on the parameters (URLs) You input --> obtain data --> select a View for output.
After the Action is executed, an ActionResult is returned (generate html, page Jump ...)
Controller is a class, and Action is a method.
Conetroller requirements:
1>. inherit from the System. Web. Mvc. Controller class
2>. End with Controller
3>. The XyzController name is Xyz --> the Convention is better than the configuration.
4>. ASP. net mvc Framework searches for the Controller in all programs and scopes referenced by Web applications.

Action requirements:
Must be a public Method
Return the ActionResult type. You can also return void. Directly use Response. Write
Must be an instance method
Generic methods cannot be used.
NonActionAttribute not marked
Cannot be overloaded

ActionResult: an abstract class that indicates the execution result of the Action. The ASP. net mvc Architecture will Execute the Execute method after obtaining the ActionReault.
Built-in ActionResult: ActionRedirectResult: Jump to an Action. HttpRedirectResult: jump to any address. RenderViewResult: The generated content. EmptyResult: do nothing. JsonResult: used for JSON serialized output. ContentResult: directly outputs a string.
 

Generate View
ViewEngine (indicating which View is used for generating), which can be replaced.
Aspx, ascx, and master are used by default.
MvcContrib provides four additional viewengines.
Default View of ASP. net mvc:. aspx -- Subject of the page content,. master -- template framework,. ascx -- local View: Html. RenderUserControl method generates a local View of a UserControl.
Must Inherit System. Web. Mvc. ViewPage
-- ViewData has a property of "Model ".
-- ViewPage <T> ViewData is of the T type.
-- The default ViewPage is similar to ViewPage <object>
How to generate html in View:
1>. inline Script: for {}, foreach {}. Similar to ASP, but not the business logic and the html displayed are put together. The two are still separated.
The biggest advantage of using scripts is flexible usage. Isn't debugging very convenient ???
2>. Use Controls: Repeater (recommended) can be used to display one-dimensional data, and ListView (recommended) can be used to display two-dimensional data.
3>. Auxiliary Methods
-- HtmlHelper class: Some auxiliary methods are provided to generate the HTML of Form, ActionLink, Button, CheckBox, Select (drop-down box), Encode...
Method.
-- UrlHelper class: Generate Url and Url-related things. For example, generate the Action Url.

Summary: I have a preliminary understanding of the basic concepts and execution processes of ASP. NET MVC.

Original article: http://www.cnblogs.com/gaoweipeng/archive/2009/08/12/1543773.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.