Document directory
- 1. Search for the portal
- 2. Search for Controller
- 3. query Action
- 4. Search for View
- 5. Page display
Learning ASP. net mvc 1.0 from scratch
- (1) getting started
- (2) identify the Routing component of a URL
- (3) in-depth analysis of Controller/Action and Application Instances
- (4) View/Model full solution
- (5) in-depth analysis of ViewEngine and Application Instances
I. Summary
With ASP. net mvc 1.0 was officially released. I updated this series of articles to 1.0. I will re-edit published articles according to Version 1.0. I hope this series of articles can help you.
Ii. Preface
ASP. net mvc is an open-source MVC Framework officially provided by Microsoft. after a long preview and two RC versions, we finally released Version 1.0. in fact, ASP. net mvc core code has become stable. so now I am learning ASP. net mvc is a good time point. learning is early, and you don't have to worry about outdated knowledge.
In this series of tutorials, I am learning ASP. net mvc summary, for example, although I have studied Zhao's video tutorial, but because the version is older, I usually need to verify and sublimate the knowledge from the latest version, then sum up the final essence and write it into text. I am still learning and writing. I hope everyone will like this series of articles. I will write this series of articles with the spirit of perfection.
3. Hello ASP. NET MVC
Now I start to learn ASP step by step. net mvc. before we know nothing, let's download an ASP first. net mvc program. when I see the program running, my mind will be much more steadfast.
First, go to ASP. net mvc's official website to see:
Http://www.asp.net/mvc/
Here, we can download the Installation File of ASP. net mvc. The latest version is 1.0. In addition, we can obtain the source code on CodePlex:
Http://www.codeplex.com/aspnet
Download the ASP. net mvc installation file from the official website, and press enter to complete the installation. Start VS2008 SP1 (not VS2008? Not SP1? As a professional IT person, the level of food is good. We must use new software! Find a new version !), Click new. You can find it on the Web page:
Click OK to create a default MVC project. A test project is also created. I don't know and don't pay attention to these issues now. set aspx as the startup page, run the project, an ASP. net mvc project is already running on my computer:
You can also click the Home and About! Although I don't understand anything now, I feel more steadfast when I look at the instances that can run!
4. Learn the concept of MVC
An ASP. NET page usually needs to do the following:
- Obtain the data required for a page.
- Bind data to the page control in the Page_Load (page loading) method of the page
- Browser display page
MVC is Model, View, Controller
The Model is the data required for the webpage we obtained in 1.
Controller is the business process in which we get the data and bind the data to the page control. This is incorrect, but we can understand it as follows: Controller is our Page_Load method logic.
View is our aspx page. Note that this is An aspx page that does not contain background code files. (In fact,. asp. there will be no compilation errors in the cs file, but writing code in this way violates the original design intention of MVC)
The following figure summarizes the role of MVC:
A URL request, ASP. net mvc engine will analyze the URL to use the Controller. The Controller (actually the actual method is the Controller Action) gets data from the database or other data sources, normally, the data is a business Model class (Model in MVC ). the Controller passes the Model object to the page (View in MVC), and the page is displayed in the browser. (This step is the ViewEngine function. Generally, the ASPX page uses the WebForm ViewEngine, which can also be replaced .)
5. Learning instance Homepage
The simple concept is also available. The instance can also run. Now let's take a look at how this instance uses ASP. net mvc. Start from the homepage.
1. Search for the portal
The address of the home page website should be http: // localhost: 1847/home/index (1847 is the port number, which is randomly generated, so it will be different ), why the address is not a specific page, but the request is finally submitted to view/home/Index. aspx page? Obviously, URL rewriting is used. ASP. NET is called UrlRouting, and the corresponding assembly is System. web. routing, open the project's Global. asax. the cs file will find the page rewriting rule we created:
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); }
An in-depth understanding of Routing will be introduced later. Here we can add new rewrite rules according to the instance.
Finally, http: // localhost: 1847/home/index is parsed:
The Controller is Home, the Action is Index, and there is no id parameter.
2. Search for Controller
In the Controllers folder, We can find HomeController. cs, the Convention is used here, that is, if the Controller name obtained in the URL is Home, its Controller class name is HomeController. add "Controller" to the name of the URL ".
The Controllers in the instance are all placed in the Controllers folder, so we can easily find the HomeController class according to the naming conventions.
3. query Action
Open HomeControllers. cs and find two methods:
public ActionResult Index() { ViewData["Message"] = "Welcome to ASP.NET MVC!"; return View(); } public ActionResult About() { return View(); }
Both Index and About are Action. What is this Action? Currently, I only know that a Controller can contain multiple actions. The specific implementation logic of the Controller role in MVC mode is in Action.
Because our Action is Index, we naturally need to call the Index () method. Here we will "Webcome to ASP. net mvc! ", Here the Model role is this sentence, that is, a string object.
An important role of Controller is to pass the Model object to the View, that is, the specific page. the transfer method is to put the string in the ViewData collection object, and then call this object on the page to obtain data. this ViewData object must be static or at least associated with the HttpContext object. Otherwise, it cannot be called on the View page. but I do not know ASP. when does net mvc create this object. go to the source code to find the answer.
4. Search for View
Now that the Model has the data, you need to jump to the View to display the data. The first question is how to jump from the Controller to the View? Return View () implements this function. In fact, if you write this statement, you will be more clear:
public ActionResult Index() { ViewData["Message"] = "Welcome to ASP.NET MVC!"; return View("index"); }
You can include a name in the View method. This name is the View name. If you change the index to about, access/home/index to go to The about page!
Although we know that we want to return to the View named "index", where is the View page? There is a view folder in the Web:
View objects are stored here, that is, only the display function's aspx page files. however, the aspx file must follow the Conventions: Create a folder under Views according to the Controller, for example, HomeController corresponds to the Home folder, and then add the view, such as index. aspx, the View object named Index returned in HomeController will actually return Views/Home/Index. aspx page.
If the View name is not specified, the Action name is the View name.
5. Page display
The final task is to use the Model data to display the complete page on the View page. In the View object without background code index. aspx, you can get the Model through the ViewData object:
<%= Html.Encode(ViewData["Message"]) %>
Next, ViewEngine, that is, the page engine, returns the HTML part in aspx along with the preceding data part to the browser.
About the View object, I noticed that this page is inherited from System. web. mvc. viewPage instead of directly inheriting from System. web. UI. page, and this ViewData object is an attribute in ViewPage. the ViewData here must be at the page level. After the page is compiled, this object will be canceled (HTTP is a stateless protocol, and every request generates a new ViewPage object ).
Vi. Summary
Although it is only the first entry-level article, Do you think ASP. net mvc will be used? Although you cannot be proud of learning, you can be proud of ASP. net mvc I am no longer familiar with it, even if I am not familiar with it, I occasionally come up with a few concepts, to explain to you what MVC is, it is still very scary. in subsequent articles, I will explain the various details of MVC. I hope you will like this series of articles!