Source: Internet
Author: User

What is the MVC mode?

MVC (Model-View-controller, Model-View-controller mode) is used to represent a software architecture mode. It divides a software system into three basic parts: model, view, and controller ).

So what is the difference between the MVC mode and the webform mode we are familiar? What is his division of labor?

Let's take a look at the normal webform mode. We request a URL such as, and then our webformProgramGo to the root directory of the website to find the index under the blog directory. the aspx file is then created by index. the codebehind file (. CS files) for logical processing, which may also include retrieving data from the database (The bll in the database will not be discussed here), and then the index. the ASPX page is displayed to the user. The following is a simple example:


That is, a URL request is a physical file (aspx file or other) on the server and corresponding to the URL. Then, the file processes the request and returns the result to the client.

However, what is the process of MVC mode?

Let's first create an ASP. net mvc project. Vs2008 by default is no ASP. net mvc Project template, first we need to Familyid = A24D1E00-CD35-4F66-BAA0-2362BDDE0766 & displaylang = en to download the latest ASP. net mvc installer, the latest version of Microsoft ASP. net mvc beta (10/15/2008 ). After the download and installation are complete, we can find the ASP. net mvc project in the new project:

Note: If you are using the Chinese version of Vs, you may not be able to find this template after installation. For details, refer to installing MVC in the Chinese version of vs 08.ArticleSet it.

After an ASP. net mvc project is created, the default project is roughly as follows:


We can see that the naming of Several folders in the project corresponds to MVC (Model-View-controller, Model-View-controller mode. Run the following project:


We noticed that the URL in the address bar is home/index. If we follow the webform mode described above, we should be able to find the home directory under the root directory of our project, then there is an index file in the home directory, but we cannot find the home directory in the root directory. However, let's find the views/home/index. aspx file in the Views directory. Let's enter this address to run it:


Oh, no! The path is correct, and the file exists, but why is it 404? If it is not a direct access to an existing physical file, how does MVC work?

It turns out that the MVC mode works like this:


In MVC, the requested URL of the client is mapped to the corresponding controller. Then, the controller processes the business logic and may retrieve data from the model, then, the Controller selects the appropriate view and returns it to the client. Let's go back to the http: // localhost: 2176/home/index URL accessed by the ASP. net mvc program we run. It actually accesses the index action in homecontroller. For details, see:


The public actionresult index () method is called the Controller action, which returns the type of actionresult. A controller can have multiple actions.

How is a URL located in the controller? Let's take a look at the web. config file. In the httpmodules configuration section of the web. config file, we can see a urlroutingmodule:

<Add name = "urlroutingmodule" type = "system. Web. Routing. urlroutingmodule, system. Web. Routing, version =, culture = neutral, publickeytoken = 31bf3856ad364e35"/>

This urlroutingmodule locates the URL in the controller. We can define which controller the URL will be routed. Let's take a look at the Global. asax file:


We can see that a route named "default" is defined here, and the default parameter is also defined. The default parameter indicates that when we access a URL such as http: // localhost: 2176/, the parameter does not exist, that is, it is equivalent to accessing http: // localhost: 2176/home/index.

Note: When we access the root directory of the website in IIS, if we do not specify the path to access, IIS will access the site based on the default document set in IIS. For example, when we access http: // localhost: 2176/, IIS will find the default under the root directory of the website. aspx file (assume that the default file for IIS is set to default. aspx ). In ASP. net MVC does not process the path of the website root directory such as http: // localhost: 2176/. Therefore, we can see the created ASP. net MVC program has a default under the root directory. aspx file, which is used to process the previous access to the root directory. Do not delete this file. It submits http: // localhost: 2176/default. aspx to ASP. net mvc for processing. For details, see the default. aspx. CS file.

We know how a URL is located in the corresponding controller, so how is the view returned to the client? We can see from the previous section that the action method in controller has a return view () method. By default, the view with the same name as the action is returned. Under the default view engine (webformviewengine) of ASP. net mvc, the view is accessed in the following path:

/Views/{controller}/{action}. aspx

That is, for the http: // localhost: 2176/home/index path, when return view () is used in the index action by default, will look for/views/home/index. aspx file. If this file cannot be found, it will be searched in the share Directory:/views/share/index. aspx. If no view is found, an exception occurs. Return view ("Lulu. aspx") to specify which view to return:/views/home/Lulu. aspx.

So why does the 404 error occur when we directly access the files in views/home/index. aspx? In MVC, it is not recommended to directly access the view, so the created ASP. by default, the net MVC program adds a web under the views directory. the config file contains the following content:


That is to say, access to all the files under the views directory will be handled by system. Web. httpnotfoundhandler. Therefore, do not put the resource files (CSS, JS, images, etc.) into the views directory. If you really want to put it in the Views directory, modify the views/Web. config file.

Now, you should have a rough understanding of the working principle of MVC. Let's talk about it first. Enjoy!


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: 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.